If we want to store information of many students we need to use array of structure. To store large number of similar records C allows us to create an array of structure variables.
Using array of structure we can easily and efficiently handle large number of records.
All array elements of structure occupy consecutive memory locations.
Example:
struct StudentInfo
{ int Rno;
char Sname[20];
int marks[4];
float avg;
};
struct StudentInfo s[10]; // Here ‘s’ is an array of structure which can store 10 students record.
We can also have array within structure, in above example ‘marks’ is an array within structure StudentInfo. Such members can be accessed by using appropriate subscripts.
Example: struct StudentInfo s[10]; Rno Sname Percentage s[0] S[1] s[9].
Accessing elements of the array:
Individual elements can be accessed as
s[0].Rno, s[0].Sname, s[0].Percentage
Initializing array of structures
An array of structures contains multiple structure variables and can be initialized as shown:
struct StudentInfo s[4]=
{1, ”ABC”, 89,
2, ”DEF”, 64,
3, ”GHI”, 75,
4, ”JKL”, 90,
}
s[0] to s[3] are stored sequentially in memory.
Comments
Post a Comment
Please give us feedback through comments