Table of Contents
Array of Structure in C Language
- An array of structures in C Language can be seem as the collection of multiple structures variables where each variable contains information about different objects
- Array within structures in C are used to store information about multiple entities of different data types.
- The array of structures is also known as the collection of structures entities.
- Array of structure is declared as
struct structurename array[size];
For example If we want to store and access the information of 4 students about their roll number , name then we can do this using array of structure in following way.
Why to Use an Array of Structures in C Language ?
The main advantage of using array of structure in c Language is that we can represent multiple values with a single variable.
So the reusability of code improves; also, readability is increased.
If there is no array of structures, we need to store many values in multiple structure variables, which is redundant.
Program for Array of Structure in C
we can understand the concept of array of Structure in C with the help of following program.
Suppose we have to store the record of four students about their name and rollno the we can implement this program using array of structure in following way.
Program for array of structure is given below
#include<stdio.h>
#include <string.h>
struct student
{
int rollno;
char name[10];
};
void main( )
{
int i;
struct student st[4];
printf(“Enter Records of 4 students”);
for(i=0;i<4;i++)
{
printf(“\nEnter Rollno:”);
scanf(“%d”,&st[i].rollno);
printf(“\nEnter Name:”);
scanf(“%s”,&st[i].name);
}
printf(“\nStudent Information List:”);
for(i=0;i<4;i++)
{
printf(“\nRollno:%d, Name:%s”,st[i].rollno,st[i].name);
}
getch();
}
Output
In the above program first we defined a structure named as Student. This structure has two members roll no and name.
We want to store the information about roll no and name of 4 students so we defined an array of structure st[4] .
To associate and access the roll no and name with students we used dot . operator.
In this way we can use array of structure to store information about more than one entities having same attributes(structure members).
When we execute the above program then we found the output as shown in picture
Conclusion and Summary
- Concepts of Array of structure is explained in this tutorial with example and program.
- Array of structure is more efficient than declaring multiple variables.
- An array of structures written in C can be seem as a collection of various structure variables containing data about different entities.