• A structure can also contain array members just like normal members such int, float
  • We can declare an array if we need to store multiple values inside structure.
  • Structure may include as many array members as we require.
  • Syntax for declaring array within structure is not different than the conventional syntax. The only difference is that it is declared inside the structure.
  • Consider the following example for storing student’s information where we have declared an array of mark to store marks of six subjects and display on the screen.
  • Since we need to store marks for six subjects, we have to use looping structure but the same is not true for character array of one dimension.
Array within Structure Example:
#include "stdio.h"
#include "conio.h"
#include "string.h"

struct student
{
	int rno;
	char name[20]; // Character array
	int mark[6];  // Integer Array with six elements 
	float per;
};
void main()
{
   struct student s1;   // structure variable declaration
   int tot = 0;
   
   clrscr();

   // individual member initialization.
   
   printf(“\n Enter Student Roll No :”);
   scanf(“%d”, &s1.rno);
   printf(“\n Enter Student Name :”);
   gets(s1.name);

   // read marks for six subjects using loop
  
   for(i = 0 ; i < 6 ; i++ )
   {
      printf(“\n Enter Subject %d mark :”,i+1);
      scanf(“%d”, &s1.mark[i]);
      tot = tot + s1.mark[i];
   }
   
   // calculate percentage
   
   s1.per = tot/6 ;
   printf(“\n Roll No : %d\t Name : %s”,s1.rno, s1.name);
   
   for( i = 0 ; i < 6 ; i++ )  // display marks
   {
       printf(“\n%d”,s1.mark[i]);
   }
   printf(“\nTotal : %d\t Percentage : %.2f”,tot, s1.per );
   getch();
}