C Array of Structures
C Array of Structures
C Array of Structures
we have stored data of 3 students in the structure. However, the complexity of the
program will be increased if there are 20 students. In that case, we will have to declare 20
different structure variables and store them one by one. This will always be tough since
we will have to declare a variable every time we add a student. Remembering the name
of all the variables is also a very tricky task. However, c enables us to declare an array of
structures by using which, we can avoid declaring the different structure variables;
instead we can make a collection containing all the structures that store the information
of different entities.
• An array of structures 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.
#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);
}
return 0;
}
Output:
Below is a demonstration of 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.
#include <stdio.h>
struct candidate { // Driver Code
int roll_no; int main()
char grade; {
// Initialize a structure
// Array within the structure struct candidate A = { 1, 'A', { 98.5, 77, 89, 78.5 } };
float marks[4];
}; // Function to display structure
void display(struct candidate a1) display(A);
{ return 0;
printf("Roll number : %d\n", a1.roll_no); }
printf("Grade : %c\n", a1.grade);
printf("Marks secured:\n");
int i;
int len = sizeof(a1.marks) / sizeof(float);
for (i = 0; i < len; i++) {
printf("Subject %d : %.2f\n", i + 1,
a1.marks[i]);
}
}
Output
Roll number : 1
Grade : A
Marks secured:
Subject 1 : 98.50
Subject 2 : 77.00
Subject 3 : 89.00
Subject 4 : 78.50
Difference between Array of Structures and Array within Structures
Below is the tabular difference between the Array within a Structure and Array of Structures:
Arun
Delhi
110001
1234567890
name: Arun
City: Delhi
Pincode: 110001
Phone: 1234567890
The structure can be nested in the following ways.
1. By separate structure
2. By Embedded structure
1) Separate structure
Here, we create two structures, but the dependent structure should be used inside the
e1.doj.dd
e1.doj.mm
e1.doj.yyyy
#include <stdio.h> int main( )
#include <string.h> {
struct Employee //storing employee information
{ e1.id=101;
int id; strcpy(e1.name, "Sonoo Jaiswal");//
char name[20]; copying string into char array
struct Date e1.doj.dd=10;
{ e1.doj.mm=11;
int dd; e1.doj.yyyy=2014;
int mm;
int yyyy; //printing first employee information
}doj; printf( "employee id : %d\n", e1.id);
}e1; printf( "employee name : %s\n", e1.name);
printf( "employee date of joining (dd/mm/yyyy) : %d/%d/%d\
n", e1.doj.dd,e1.doj.mm,e1.doj.yyyy);
return 0;
}
Output:
employee id : 101
employee name : Sonoo Jaiswal
employee date of joining (dd/mm/yyyy) : 10/11/2014
Passing structure to function
Just like other variables, a structure can also be passed to a function. We may pass the structure
members into the function or pass the structure variable at once. Consider the following example to pass
the structure variable employee to a function display() which is used to display the details of an
employee.
#include<stdio.h> void main ()
struct address {
{ struct employee emp;
char city[20]; printf("Enter employee information?\n");
int pin; scanf("%s %s %d %s",emp.name,emp.add.city, &emp.add.pi
char phone[14]; n, emp.add.phone);
}; display(emp);
struct employee }
{ void display(struct employee emp)
char name[20]; {
struct address add; printf("Printing the details....\n");
}; printf("%s %s %d %s",emp.name,emp.add.city,emp.add.pin,e
void display(struct employee); mp.add.phone);
}