Monday 2 April 2018

Structure

Arrays are used to store large set of data and manipulate them but the disadvantage is that all the elements stored in an array are to be of the same data type. If we need to use a collection of different data type items it is not possible using an array. When we require using a collection of different data item of different data types we can use a structure. Structure is a method of packing data of different types. A structure is a convenient method of handling a group of related data items of different data types.
 structure definition:
 struct tag_name
 {
 data type member 1;
 data type member 2;
 ..................;
 ...................;
 };

Example :
  struct lib_books
  {
  char title[20];
  char author[15];
  int pages;
  float price;
  };

The keyword struct declares a structure to hold the details of four fields namely title, author pages and price. These are members of the structures.Each member may belong to different or same data type. The tag name can be used to define objects that have the tag names structure. The structure we just declared is not a variable by itself but a template for the structure.
         We can declare structure variables using the tag name anywhere in the program.

For example the statement,
  struct lib_books book1,book2,book3;
  declares book1,book2,book3 as variables of type struct lib_books each declaration has four elements of the structure lib_books.The complete structure declaration might look like this

struct lib_books
{
char title[20];
char author[15];
int pages;
float price;
};
struct lib_books, book1, book2, book3;

Giving values to members
 As mentioned earlier the members themselves are not variables they should be linked to structure variable in order to make them meaningful members. The link between a member and a variable is established using the member operator'.' which is known as dot operator or period operator.
   For example :
    Book!.price
Is the variable representing the price of book1 and can be treated like any other ordinary variable. We can use scanf statement to assign values like

scanf("%s", book1.file);
scanf("%d",&book1.pages);
    OR
we can assign variables to the members of book1
strcpy(book1.title, "basic");
strcpy(book1.author, "Irawen");
book1.pages = 250;
book1.price = 28.50;

 

0 Comments:

Post a Comment

Popular Posts

Categories

AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (113) C (77) C# (12) C++ (82) Course (60) Coursera (176) coursewra (1) Cybersecurity (22) data management (11) Data Science (85) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (18) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (43) Meta (18) MICHIGAN (4) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (726) Python Coding Challenge (170) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses