Arrays Of Structures


Usually, we need a number of records of any kind of structure we declare. Suppose we need a class record consisting of student’s name, roll number, age for 60 students. It is difficult to declare 60 structure variables.

In order to overcome this difficulty we declare an array structure variables. This implies that we store the information of 60 students under a same structure variable name but with different subscript values.
struct class student[60];
These can be accessed as follows:

student[i].sname, student[i].sno, student[i].age.

Write a program to compue the monthly pay of 100 employees using each employee’s name, basic pay, DA is computed as 52% of basic salary.

#include<stdio.h>
#include<conio.h>
void main()
{
   struct employee
   {
      char ename[20];
      int bp;
      float da;
      float gs;
   }emp[100];
   int i;
   for(i=0;i<100;i++)
   {
     printf("\n Enter Emp name:");
     gets(emp[i].ename);
     printf("\n Enter Emp Basic pay:");
     scanf("%d",&emp[i].bp);
     emp[i].da= 0.52*emp[i].bp;
     emp[i].gs= emp[i].bp+emp[i].da;
   }
   clrscr();
   for(i=0;i<100;i++)
   {
     printf("\n Emp Name      : %s",emp[i].ename);
     printf("\n Emp Basic pay : %d",emp[i].bp);
     printf("\n Emp DA        : %f",emp[i].da);
     printf("\n Emp GS        : %f",emp[i].gs);
   }
   getch();
}


Write a program to create an array of student structure objects and to find the highest marks scorer. The fields in the student structure are name,age,marks.

#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
 int sno;
 char sname[20],result[20];
 int marks;
}st[20];
void main()
{
	  int n,i,nop=0;
	  clrscr();
	  printf("\n Enter Number of Records :");
	  scanf("%d",&n);
	  for(i=0;i < n;i++)
	  {
	    printf("\n Enter Student Name :");
	    scanf("%s",st[i].sname);
	    printf("\n Enter Student Number :");
	    scanf("%d",&st[i].sno);
	    printf("\n Enter Student Marks:");
	    scanf("%d",&st[i].marks);
	  }
	  clrscr();
	  for(i=0;i<n;i++)
	  {
	     if(st[i].marks>35)
	     {
	       strcpy(st[i].result,"PASS");
	       nop++;
	     }
	     else
	     strcpy(st[i].result,"PASS");
	  }
	  printf("\n Number of Students Passed : %d",nop);
  getch();

}

Arrays within Structures:

We can use arrays as structure members. This serves us the need when we need to store many fields of same type of a particular person, say marks of 5 subjects of a student.
For example

struct marks
{
    int number;
    float subject[5];
 }student[2];


Student1’s subject marks can be accessed as
student[0].subject[0];
student[0].subject[1];
student[0].subject[2];
and similarly for student2 with student index 1;

Nested Structures

Nested structures indicates that a structure itself contains another structure nested (included) in it. Suppose we need to write a program in which a student details are to be read containing name, age, date of birth..

We can take all data related to date i.e, day, month, year in one separate structure as shown below.

#include< stdio.h>
#include<conio.h>
void main()
{
	struct stud
	{
	      char name[20];
	      int age;
	      struct dob
	     {
	          int dd,mm,yy;
	      }d;
	};
	struct stud s;
	clrscr( );
	printf("\n Enter student name:");
	gets(s.name);
	printf("\n Enter age :");
	scanf("%d",&s.age);
	printf("\n Enter his date of birth :");
	scanf("%d%d%d",&s.d.dd,&s.d.mm,&s.d.yy);
	clrscr();
	printf("\n Student name :%s",s.name);
	printf("\n Student age    :%d",s.age);
	printf("\n Date of Birth  : %d-%d-%d",s.d.dd,s.d.mm,s.d.yy);
	getch();
}


The inner structure can also be defined outside and included in the outer structure with an object declared. This can be done as follows

struct dob
{
    int dd,mm,yy;
}; 

struct stud
{
   Char name[20];
   int age;
   struct dob d;
};		


Accessing the members of inner structure is same using two dot (.) operators.