We can also use a Array as Structure variable is called Array of Structure.
Syntax:
struct structure_name
{
data_type array_variable1;
data_type array_variable2;
---------------------------------
data_type array_variableN;
}structure array_variable ; // Global Array variable Declaration.
OR
struct structure_name structure_array_variable; // Local Array Variable declaration.
Example :
struct student
{
char name[20];
int roll;
float marks;
}s[10]; // Global Array variable Declaration.
OR
struct student s[5]; // Local Array Variable declaration.
Example of Array of Structure:
#include<stdio.h>
#include<conio.h>
//Structure is Created with Array
struct abc
{
char name[20];
int rollno;
int per;
};
// Initialization a Structure
void main()
{
int i;
struct abc s1[5];
clrscr();
for(i=0;i<5;i++)
{
printf("Enter the Information of Student No. %d",i+1);
printf("\n Enter the Name of the Student-");
scanf("%s",s1[i].name);
printf("\n Enter the Roll No. of the Student-");
scanf("%d",&s1[i].rollno);
printf("\n Enter the Percentage of the Student-");
scanf("%d",&s1[i].per) ;
}
// Accessing structure member
for(i=0;i<5;i++)
{
printf(" \nThe Information of Student No. %d",i+1);
printf("\n Name of the Student--%s",s1[i].name);
printf("\n Roll No. of the Student--%d",s1[i].rollno);
printf("\n Percentage of the Student--%d",s1[i].per);
}
getch();
}
Syntax:
struct structure_name
{
data_type array_variable1;
data_type array_variable2;
---------------------------------
data_type array_variableN;
}structure array_variable ; // Global Array variable Declaration.
OR
struct structure_name structure_array_variable; // Local Array Variable declaration.
Example :
struct student
{
char name[20];
int roll;
float marks;
}s[10]; // Global Array variable Declaration.
OR
struct student s[5]; // Local Array Variable declaration.
- Here, s[10] is a variable of structure student and store the information of 10 students on index s[0],s[1],s[2],…s[4].
- Every index have a information of a student.
- These elements can be accessed with its appropriate subscripts/indexes.
Example of Array of Structure:
#include<stdio.h>
#include<conio.h>
//Structure is Created with Array
struct abc
{
char name[20];
int rollno;
int per;
};
// Initialization a Structure
void main()
{
int i;
struct abc s1[5];
clrscr();
for(i=0;i<5;i++)
{
printf("Enter the Information of Student No. %d",i+1);
printf("\n Enter the Name of the Student-");
scanf("%s",s1[i].name);
printf("\n Enter the Roll No. of the Student-");
scanf("%d",&s1[i].rollno);
printf("\n Enter the Percentage of the Student-");
scanf("%d",&s1[i].per) ;
}
// Accessing structure member
for(i=0;i<5;i++)
{
printf(" \nThe Information of Student No. %d",i+1);
printf("\n Name of the Student--%s",s1[i].name);
printf("\n Roll No. of the Student--%d",s1[i].rollno);
printf("\n Percentage of the Student--%d",s1[i].per);
}
getch();
}
Output: