Arrays of Structures
Let us consider the above-mentioned example (of employees in an organization) to illustrate this concept. Assume there are five employees in an organization whose records are to be maintained. The declaration goes like this
The declaration defines five instances of the variable emp1, of type struct employee. They could be initialized as
struct employee
{
char *name;
char *rollno;
int salary;
}emp1[5] =
{
{"Jame s", "e01", 2000},
{"Jerry", "e02", 4100},
{"Sheryl", "e03", 3000},
{"Meera", "e 04 ", 2500},
{"Veena", "e 05 ", 5000}
}
As in the case of other datatypes, the index 5 does not have to be specified, as the initialization by itself will determine its size.
Notice that in the initialization part of the above declaration, every particular employee"s details have been enclosed in braces. This is not necessary, but it improves clarity of code. In case a member is not to be initialized. the value for it can be omitted. For example, assume the name and salary of an employee are not known, the declaration will be
{ , e06, }
Any member of the structure can be accessed using the index number along with the structure name. For example, if one wants to print the name of the first employee,
printf ("The name of the first employee is %s", emp1[0].name) :
Note: