logo
Array of structures In C Language
A structure is simple to define if there are only one or two element, but in case there are too  many objects needed for a structure, for ex. A structure designed to show the data of each student of the class, and then in that case, the way will be introduced. A structure declaration using array to define object is given below.
Structures
Initializing array of structures :

It is to be noted that only static or external variable can be initialized.

Structures

If incase, the structure of object or any element of struct are not initialized, the compiler will automatically assigned zero to the fields of that particular record.

Example :

employee data [3] = { { 146,’m’} ,{ 200, ‘f’ },{250 ,’m’}};

Compiler will assign :

data [0].sal=0; data [1].sal=0; data [2].sal=0;

Program :The program to initialize the array of structure members & display
#include<stdio.h>
void main ()
{  
int i;
   struct student 
{ 
   long int rollno;
   char sex; 
   float height;
   float weight; 
};
    struct student data [3] = {  {121,’m’,5.7,59.8},{122,’f’,6.0,65.2},{123,’m’, 6.0, 7.5} };
    clrscr ();
    printf (“the initialized contents are:\n”);
for ( i=0; i< =2; i++)
   {  
printf (“%d/n   ** Record is  \n “, data [i].rollno);
printf (“%c\n“,  data [i] .sex);
printf (“%f\n”,  data [i].height);
printf (“%f\n”,  data [i]. weight); 
    }
}
Output :

Output: The initialized contents are:
121
** Record is
m
5.700000
59.799999
122
** Record is
f
6.00000
65.19997
123
** Record is
m
6.000000
7.500000