Array of Structure : C Programming
Array of Structure :
Structure is used to store the information of One particular object but if we need to store such 100 objects then Array of Structure is used.
Example :
struct Bookinfo { char[20] bname; int pages; int price; }Book[100];
Explanation :
- Here Book structure is used to Store the information of one Book.
- In case if we need to store the Information of 100 books then Array of Structure is used.
- b1[0] stores the Information of 1st Book , b1[1] stores the information of 2nd Book and So on We can store the information of 100 books.
book[3] is shown Below
Accessing Pages field of Second Book :
Book[1].pages
Live Example :
#include <stdio.h> struct Bookinfo { char[20] bname; int pages; int price; }book[3]; int main(int argc, char *argv[]) { int i; for(i=0;i<3;i++) { printf("\nEnter the Name of Book : "); gets(book[i].bname); printf("\nEnter the Number of Pages : "); scanf("%d",book[i].pages); printf("\nEnter the Price of Book : "); scanf("%f",book[i].price); } printf("\n--------- Book Details ------------ "); for(i=0;i<3;i++) { printf("\nName of Book : %s",book[i].bname); printf("\nNumber of Pages : %d",book[i].pages); printf("\nPrice of Book : %f",book[i].price); } return 0; }
Output of the Structure Example:
Enter the Name of Book : ABC Enter the Number of Pages : 100 Enter the Price of Book : 200 Enter the Name of Book : EFG Enter the Number of Pages : 200 Enter the Price of Book : 300 Enter the Name of Book : HIJ Enter the Number of Pages : 300 Enter the Price of Book : 500 --------- Book Details ------------ Name of Book : ABC Number of Pages : 100 Price of Book : 200 Name of Book : EFG Number of Pages : 200 Price of Book : 300 Name of Book : HIJ Number of Pages : 300 Price of Book : 500