Array of Structure


Array of a structure refers to an array in which each element is a structure, having same named members and of the same data type. Thus instead of declaring 100 different variables we can use an array having 100 elements.
            The following example illustrates how to define an array of structure and access their member. Consider an example of a student structure declaration.
struct student
{
            int rollno;
            char name [20];
            float marks ;
}
Suppose you want to deal with information pertaining to 15 students so you would have to create an array of student structure, instead of defining separate structure variables. It is done as follows,
student s [15];
The above definition defines an array of structure s. The array has 15 students’ type elements. The first student type variable can be referred by s [0], second by s [1] and so on. The last student type variable can be referred by s [14]. The individual members can be accessed in the similar manner. So to access rollno member of second student we have to write
s [1].rollno.
Similarly

 s [0].marks; /* accessing marks of ist student

Previous                                                                                    Next


Powered by Blogger.