Write about Arrays of Structures?


We use structures to describe the format of a number of related variables.

For ex: in analyzing the marks obtained by a class of students, we may use a template to describe student name and marks obtained in various subjects and then declare all the students as structure variables.

In such cases, we may declare an array of structures, each element of the array representing a structure variable.

Ex: struct class stu [100];

Declares an array called stu that consists of 100 elements.

Ex :

struct marks
{
int sub 1;
int sub 2;
int sub 3;
};

main ( )
{
struct marks student [3]={{4,5,6,8,8,7}, {75,53,69}, {57,36,71}};
}

This declares the student as an array of 3 elements student [0], student [1],& student [2]& initializes their members as

Student [0].sub1 = 45;
Student [0].sub 2=68;
.
.
.
Student [2] .sub 3=71;

Arrays Within Structures:

C permits the use of arrays as structure members. We have already used arrays of characters inside a structure. Similarly, we can use single dimensional or multi dimensional arrays of type int or float.

Ex:

strut marks
{
int no;
float sub [3];
}
student [2];

Here, the member sub contains 3 elements, sub [0], sub[1] and sub[2]. The elements can be assessed using appropriate subscripts.

Ex: student [1].sub[2];

Would refer to the marks obtained in the 3ed subject by the 2nd student.

Related

C Language 932240009501321730

Post a Comment

emo-but-icon

item