Array Within Structure
Array Within Structure
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.
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.
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.
Struct struct-name
{
datatype var1; // normal variable
datatype array [size]; // array variable
----------
----------
datatype varN;
};
struct struct-name obj;
#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("\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 class: A
Enter class: A
Access Can be accessed using the dot operator just as Can be accessed by indexing
Parameter Array within a Structure Array of Structures