Tuesday, 27 March 2012

Array of Structure




The real power in using structure comes when use an arrays of structures. By maintaining an array of structures, a database of information can be manipulated for wide range of items.
Ex     
struct stud
{
          int sno;
          char sname[15];
          int m1,m2,m3;
};

void main()
{
          struct stud s[5];
          int i;

          for(i=0;i<5;i++)
          {
                   printf(“\n Enter No :”);
                   scanf(“%d”,&s[i].sno);
                   fflush(stdin);
                   printf(“\n Enter Name:”);
                   gets(s[i].name);
                   printf(“\n Enter M1 M2 M3:”);
                   scanf(“%d %d %d”,&s[i].m1,&s[i].m2,&s[i].m3);

          }

          for(i=0;i<5;i++)
          {
                   printf(“\n No:%”,s[i].no);
                   printf(“\n Name :%s”,s[i].name);
                   printf(“\n M1  :%d”,s[i].m1);
                   printf(“\n M2 :%d”,s[i].m2);
                   printf(“\n M3 :%d”,s[i].m3);
          }
}

          As shown in above example create the array of structure stud in structure variable(object) s which holds data for five student.

Posted By : Ruchita Pandya

No comments:

Post a Comment