English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C Language Basic Tutorial

C Language Flow Control

C Language Functions

C Language Arrays

C Language Pointers

C Language Strings

C Language Structure

C Language File

C Others

C Language Reference Manual

C Language Structures and Arrays

Why use an array of structures?

For example, there is a case where we need to store5Data of a student. We can use the following structure to store it.

#include <stdio.h>  
struct student  
{  
    char name[20];  
    int id;  
    float marks;  
};  
void main()
{
	struct student s1,s2,s3;
	int dummy;
	Enter student1Name, ID card and score ");
	scanf("%s %d %f", s1.name, &s1.id, &s1.marks);
	scanf("%c", &dummy);
	Enter student2Name, ID card and score ");
	scanf("%s %d %f", s2.name, &s2.id, &s2.marks);
	scanf("%c", &dummy);
	Enter student3Name, ID card and score ");
	scanf("%s %d %f", s3.name, &s3.id, &s3.marks);
	scanf("%c", &dummy);
	printf("Output details....\n");
	printf("%s %d %f\n", s1.name,s1.id,s1.marks);
	printf("%s %d %f\n", s2.name,s2.id,s2.marks);
	printf("%s %d %f\n", s3.name,s3.id,s3.marks);
}

Output results

Enter student1Name, ID card and score James 90 90  
Enter student2Name, ID card and score Adoms 90 90  
Enter student3Name, ID card and score Nick 90 90       
Output details....        
James 90 90.000000                          
Adoms 90 90.000000                      
Nick 90 90.000000

in the above program, we stored3student data. But if there is2zero students, the complexity of the program will increase. In this case, we will have to declare2zero different structure variables and store them one by one. This will always be difficult because every time a student is added, a variable must be declared. Remembering all variable names is also a very tricky task. However, in C language, we can use an array of declarations of structures to avoid declaring different structure variables; instead, we can create a collection of structures that contains all the information about different entities.

structure array in C language

In C language, a structure array can be defined as a collection of multiple structure variables, each containing information about different entities. In C language, a structure array is used to store information about multiple entities of different data types. An array of structures is also called a collection of structures.

Let's look at the storage5of a structure array to store and print information of a student.

#include <stdio.h>  
#include <string.h>    
struct student{    
    int rollno;    
    char name[10];    
};   
int main(){  
    int i;  
    struct student st[5];  
    printf("Enter ",5student data");  
    for(i=0;i<5;i++{  
        printf("\nEnter number:");  
        scanf("%d", &st[i].rollno);  
        printf("\nEnter name:");  
        scanf("%s", &st[i].name);  
    }  
    printf("\nStudent information list:");  
    for(i=0;i<5;i++{  
        printf("\nNumber:%d, Name:%s", st[i].rollno, st[i].name);  
    }  
       return 0;  
}

Output:

Enter5student data
Enter Serial Number:1
Enter name: Sonoo
Enter Serial Number:2
Enter Name: Ratan
Enter Serial Number:3
Enter Name: Vimal
Enter Serial Number:4
Enter Name: James
Enter Serial Number:5
Enter Name: Sarfraz
Student Information List:
Serial Number:1, Name: Sonoo
Serial Number:2, Name: Ratan
Serial Number:3, Name: Vimal
Serial Number:4, Name: James
Serial Number:5, Name: Sarfraz