Arrays within structure in C programming Language.

From the given fig. let us understand how array used within structure in C programming language.
we can use single or multidimensional arrays as a structure members inside the structure of types int,char,float double etc.
Declaration and definition of array within structure is same as that of normal declaration of structure.

in the fig. we have declared structure and array within structure for three students to store theire roll number name,marks obtained in four subjects, total marks and average marks.

The declaration of array within structure is same as given in figure.
After declaraing the array within structure, we have to create the variable of structure to access the structure members.
The variable of array is created using the following method.
struct student s[3];
With the help of structure variable we can use or access the structure members or array memebers of structure.
The Roll Number of first Student can be accessed or read with the help of s[0].rollno.
Name accessed with s[0].name
Four subjects marks obtained by first Student is accessed by
s[0].sub[0]
s[0].sub[1]
s[0].sub[2]
s[0].sub[2]
Simillarly for others is the same with using respective index positions.
Simillarly to accesse others structure member the process is same as with using respective index positions.
To Access the marks obtained by second student in third subject use s[1].sub[2], simillarly marks obtained by third student in fourth subject use s[2].sub[3].

Let us undestand how to create, access array of structure and array within structure with the help of program given below.
we have used the nested for loop to achive the same in C program.


Arrays within Structure Example in C language.

The example shows how to access individual elements of structure and array within structure to find the total and average marks.

struct student
{
 int rollno;
 char name[15];
 int sub[4];
 int tot;
 float avg;
};
int main()
{
 int i;
 float avg=0.0;
 struct student s[3];
 printf(“\n Enter student Information”);
 for(i=0;i<3;i++)
 {
  printf(“\n Enter %d Student information”,i);
  printf(“\n Enter %d Student roll number\n”);
  scanf(“%d”,&s[i].rollno);
  printf(“\n Enter %d student Name\n”);
  scanf(“%s”,s[i].name);
  for(int j=0;j<4;j++)
  {
   printf(“\n Enter %d subject marks\n”,i+1);
   scanf(“%d”,&s[i].mark[j]);
   s[i].tot+=s[i].mark[j];
  }
  s[i].avg=s[i].tot/4.0;
 }
 printf(“\n Student Information\n”);
 for(i=0;i<3;i++)
 {
  printf(“\n %d Student information”,i);
  printf(“\n RollNo=%d”,s[i].rollno);
  printf(“\n Name=%s”,s[i].name);
  printf(:\n Subject Marks\n”);
  for(int j=0;j<4;j++)
  {
   printf(“%d ”,s[i].mark[j]);
  }
  printf(“\n toal=%d”,s[i].tot);
  printf(“\n Average=%.2f”,s[i].avg);
 }
 return(0); }


Output:
 Enter student Information
 Enter 1 Student information
 Enter 1 Student roll number
 101
 Enter 1 student Name
 Ajay
 Enter 1 subject Mark
 50
 Enter 2 subject Mark
 50
 Enter 3 subject Mark
 50  Enter 4 subject Mark
 50
 .
 .
 .
 Enter 3 Student roll number
 103
 Enter 3 student Name
 Raj
 Enter 1 subject Mark
 50
 Enter 2 subject Mark
 50
 Enter 3 subject Mark
 50
 Enter 4 subject Mark
 50
 Student Information
 1 Student information
 RollNo=101
  Name=Ajay
 Subject Marks
 50
 50
 50
 50
 Total=200
 Average=50.00
 .
 .
 3 Student information
 RollNo=103
  Name=Raj
 Subject Marks
 50
 50
 50
 50
 Total=200
 Average=50.00

Previous Topic:-->> Array of Structure || Next topic:-->>Structure within Structure.