Array within a Structure - C-Tutorial

Latest

Thursday, 20 October 2016

Array within a Structure

Array within a Structure


C- allows user can place the structure member as a array variable. 

syntax:
              struct struct-name
              {
                     datatype var1;                   
                     datatype array [size];          // array variable
                     - - - - - - - - - -
                     datatype varN;
              }variable_name;

Example for array within structure
       struct Student
       {
              int Roll;
              char Name[25];
              int Marks[3];             
              int Total;
              float Avg;
       }S;

       void main()
       {
              int i;
              printf("\n\nEnter Student Roll : ");
              scanf("%d", &S.Roll);
              printf("\n\nEnter Student Name : ");
              scanf("%s", &S.Name);
              S.Total = 0;
              for(i=0;i<3;i++)
              {
                     printf("\n\nEnter Marks %d : ",i+1);
                     scanf("%d",&S.Marks[i]);
                     S.Total = S.Total + S.Marks[i];
              }
              S.Avg = S.Total / 3;
              printf("\nRoll : %d",S.Roll);
              printf("\nName : %s",S.Name);
              printf("\nTotal : %d",S.Total);
              printf("\nAverage : %f",S.Avg);

       }

No comments:

Post a Comment