EXPLAIN ARRAY OF STRUCTURE WITH PROPER EXAMPLE.

Array of structures is collection of structure variables. This is also called as structure array in C.
Structure is used to store the information of One particular object but if we need to store multiple objects then Array of Structure is used
For example
struct book
{
    char title[20];
    int book_id;
    float price;
}bk1[10]; //Array of structure

struct book
{
    char title[20];
    int book_id;
    float price;
};
Inside main:
Struct book bk1[10]; //Array of structure

In case if we use to store the information of 10 books then structure of array is used.
bk[0] store the information of first book bk[1] store the information of second book and so on.

Example:
#include <stdio.h>
#include<conio.h>
struct Book
{
      charname[20];
      int id;
      int price;
}bk[3];

void main()
{
    int i;
    clrscr();
    for(i=0;i<3;i++)
    {
            printf("\nEnter the Name of Book    : ");
            gets(bk[i].name);
            printf("\nEnter the Id of Pages : ");
            scanf("%d",bk[i].id);
            printf("\nEnter the Price of Book   : ");
            scanf("%f",bk[i].price);
    } 

    printf("\n--------- Book Details ------------ ");
   

    for(i=0;i<3;i++)
    {
            printf("\nName of Book    : %s",bk[i].name);
            printf("\nNumber of Pages : %d",bk[i].id);
            printf("\nPrice of Book   : %f",bk[i].price);
    }   
    getch();
}






0 comments:

Post a Comment