Array of Structures C
An array of structres in C can be defined as the collection of multiple structures variables where each
variable contains information about different entities. The array of 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.
An array of structure in C programming is a collection of different datatype
variables, grouped together under a single name.
Array of structures
The most common use of structure in C programming is an array of structures.
To declare an array of structure, first the structure must be defined and then an array variable of
that type should be defined.
Array of Structures
An array is a collection of data items of the same type. Each element of the
array can be int, char, float, double, or even a structure. We have seen that a
structure allows elements of different data types to be grouped together
under a single name. This structure can then be thought of as a new data type
in itself. So, an array can comprise elements of this new data type. An array
of structures finds its applications in grouping the records together and
provides for fast access.
Below is a demonstration of an array of structures. The array holds the
details of the students in a class. The details include the roll number,
grade, and marks, which have been grouped under a structure (record).
There exists one record for each student. This is how a collection of related
variables can be assembled under a single entity for enhancing the clarity of
code and increase its efficiency.
Example of an array of structures that stores information
of 5 students and prints it.
#include<stdio.h>
#include <string.h>
struct student
{
int rollno;
char name[10];
};
int main()
{
int i;
struct student st[5];
printf("Enter Records of 5 students");
for (i=0;i<5;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<5;i++)
{
printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);
}
}
Array within a Structure
A structure is a data type in C that allows a group of related variables to be
treated as a single unit instead of separate entities. A structure may contain
elements of different data types – int, char, float, double, etc. It may also
contain an array as its member. Such an array is called an array within a
structure. An array within a structure is a member of the structure and can be
accessed just as we access other elements of the structure.
a program that uses the concept of the array within a structure. The
program displays the record of a student comprising the roll number, grade,
and marks secured in various subjects. The marks in various subjects have
been stored under an array called marks. The whole record is stored under a
structure called a candidate.
As we know, the structure is a collection of the different data types. Like
normal data types, It can also store an array as well. In arrays within the
structure, the member of the structure is an array.
The syntax for array within the structure:
Struct struct-name
{
datatype var1; // normal variable
datatype array [size]; // array variable
----------
----------
datatype varN;
};
struct struct-name obj;
Example program for array within structure:
#include<stdio.h>
#include<string.h>
struct Student
{
int Roll;
char Name[10];
int Marks[3]; //array of marks
int Total;
float Avg;
};
int main()
{
int i;
struct Student S;
printf("\n\nEnter Student Roll : ");
scanf("%d",&S.Roll);
printf("\n\nEnter Student Name : ");
scanf("%s",&S.Name);
S.Total = 0;
for(i=0;i<3;i++)
{
printf("\n\nEnter Marks %d : ",i+1);
scanf("%d",&S.Marks[i]);
S.Total = S.Total + S.Marks[i];
}
S.Avg = S.Total / 3;
printf("\nRoll : %d",S.Roll);
printf("\nName : %s",S.Name);
printf("\nTotal : %d",S.Total);
printf("\nAverage : %f",S.Avg);
}
#include<stdio.h>
struct student {
char name[50];
char Class[100];
int roll_number;
float marks[5];
};
int main() {
struct student s[2];
for (int i = 0; i < 2; i++) {
printf("\nEnter details of student %d\n", i +
1);
printf("Enter name: ");
scanf("%s", s[i].name);
printf("\nEnter roll no: ");
scanf("%d", &s[i].roll_number);
printf("\nEnter class: ");
scanf("%s", s[i].Class);
for (int j = 0; j < 5; j++) {
printf("\nEnter the marks in subject %d (out
of 100): ", j + 1);
scanf("%f", &s[i].marks[j]);
}
printf("\n");
}
printf("\n");
printf("Name\t\tRoll
no\t\t\tClass\t\t\t\tMarks\n");
for (int i = 0; i < 2; i++) {
printf("%s\t\t%d\t\t\t%s\t\t",
s[i].name, s[i].roll_number, s[i].Class);
for (int j = 0; j < 5; j++) {
printf("%.2f\t", s[i].marks[j]);
}
printf("\n");
}
return 0;
}
Output:
Enter details of student 1
Enter name: Aaradhya
Enter roll no: 1
Enter class: A
Enter the marks in subject 1 (out of 100): 100
Enter the marks in subject 2 (out of 100): 99
Enter the marks in subject 3 (out of 100): 98
Enter the marks in subject 4 (out of 100): 97
Enter the marks in subject 5 (out of 100): 99
Enter details of student 2
Enter name: Scaler
Enter roll no: 2
Enter class: A
Enter the marks in subject 1 (out of 100): 100
Enter the marks in subject 2 (out of 100): 100
Enter the marks in subject 3 (out of 100): 99
Enter the marks in subject 4 (out of 100): 98
Enter the marks in subject 5 (out of 100): 100
Name Roll no Class
Marks
Aaradhya 1 A 100.00 99.00 98.0
Scaler 2 A 100.00
100.00 99.00 98.00 100.00
Difference between Array of Structures and Array within
Structures
Below is the tabular difference between the Array within a Structure and Array of
Structures:
Parameter Array within a Structure Array of Structures
A structure contains an array as its member An array in which each element
Basic idea variable is of type structure
Access Can be accessed using the dot operator just as Can be accessed by indexing
Parameter Array within a Structure Array of Structures
we access other elements of the structure just as we access an array
struct class { int a, b, c; }
struct class { int ar[10]; } a1, a2, a3;
Syntax students[10];