Suppose if you want to create collection of records then we can use array of structures in C. Since array is the collection of same data type, so we use wiil structure as our user-defined data type.
Defining an array of structure
Declaring an array of structure is similar as defining a structure.
Syntax for defining an array of structure
struct struct_name{
member definition;
...
...
member definition;
}one of more structure obect[array length];
Example
struct student{
int rollno;
char name[20];
}s[10];
Accessing members of array of structure
To access member of array of structure we use member access operator(.).
Program to access members from array of structure
#include<stdio.h>
#include<string.h>
struct student{
int rollno;
char name[20];
};
int main()
{
struct student s[3];
s[0].rollno = 1234;
s[1].rollno = 1235;
s[2].rollno = 1236;
strcpy(s[0].name, "Richard");
strcpy(s[1].name, "James");
strcpy(s[2].name, "William");
for(int i = 0; i<3; i++)
{
printf("Roll no. is %d\n", s[i].rollno);
printf("Name is %s\n", s[i].name);
}
return 0;
}
Output
Roll no. is 1234
Name is Richard
Roll no. is 1235
Name is James
Roll no. is 1236
Name is William