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;
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