c array of structures
we already know about array, The Array is a collection of elements of same datatype. We can also declare array of type structures or structures where each element of array is of same structure type.
lets us take example array of structure can be declare as
.
Employee
{
name[10];
empid;
salary;
};
Employee emp[20];
|
In the above example emp is an array of 20 elements each is of structure type struct Employee
Each emp has three members which are name,empid and salary.These all members can be access by subscript notation. To access members we use .(dot) operator as we already used.
emp[0].name emp[0].empid emp[0].salary
emp[1].name emp[1].empid emp[1].salary
emp[2].name emp[2].empid emp[2].salary
emp[3].name emp[3].empid emp[3].salary
………. ………… ………….
………. ………… ………….
………. ………… ………….
………. ………… ………….
emp[19].name emp[19].empid emp[19].salary
|
/* Array of structures */
Employee
{
name[20];
empid;
salary;
};
main()
{
i;
Employee emp[10];
(i=0;i<10;i++)
{
printf("Enter the name, empid and salary");
scanf("%s %d %f",&emp[i].name,&emp[i].empid,&emp[i].salary);
}
(i=0;i<9;i++)
{
printf("%s %d %f \n",emp[i].name,emp[i].empid,emp[i].salary);
}
0;
}
|
We can also Initialize array of structure using the same syntax in Array for example
Employee emp[3]={
{“Shikha varshney”,0871,20000},
{“Noor Jahan”,0872,15000},
{“Ritu chaudhary”,0873,16000}
};
|
Array within the structure
we can have array as a members of the structure, in structure we can have member name as an array an array of characters, so now we will declare array inside the structure as member
Employee
{
name[15];
empid;
salary[5];
};
|
in this example members name and salary are Array type let show how it is allocate
if emp is variable of type struct Employee then
emp.salary[0] Denotes the first person salary
emp.salary[1] Denotes the second person salary
emp.salary[4] denotes the fifth person salary
emp.name[0] denotes the first person name
emp.name[1] denotes the second person name
emp.name[2] denotes the third person name
emp.name[14] denotes the fiftheen person name
|