We know that array is the collection of similar data type elements. If we collect data type ‘struct’ then it is known as “Array of structure”.
1 2 3 4 5 6 7 8 |
struct tag_name[optionl] { dataType member1; dataType member2; ................; ................; dataType memebrn; }structure_array_variable_Name[NUMBER_OF_STRUCTURE_ELEMENT]; |
1 2 3 4 5 6 |
struct empInfo { unsigned char name[20]; unsigned int empId; double salary; }emp[5]; |
or
1 2 3 4 5 6 7 |
struct empInfo { unsigned char name[20]; unsigned int empId; double salary; }; struct empInfo emp[5]; |
In above two structure – ’emp’ is an array of 5 ’empInfo’ type structure.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <stdio.h> struct empInfo { unsigned char name[20]; unsigned int empId; double salary; }; int main() { struct empInfo emp[3]= { {"Amit",3800209,80000}, {"Ajit",3800215,70000}, {"Ajay",3800218,60000} }; int i=0; for(i=0;i<3;i++) { printf("Name=%s\t",emp[i].name); printf("empId=%u\t",emp[i].empId); printf("Salary=%lf\n",emp[i].salary); } return 0; } |
Output:
1 2 3 |
Name=Amit empId=3800209 Salary=80000.000000 Name=Ajit empId=3800215 Salary=70000.000000 Name=Ajay empId=3800218 Salary=60000.000000 |