Array of Structure

Array of structure in C can be defined as the collection of multiple structures variables and each variable contains information about different entities. In this array of structure are used to store information about multiple entities of different data types. The array of structures is also known as the collection of structure.

Where we need to store the data of students and we can store it by using the structure as given below

#include<stdio.h>  
struct student  
{  
    char name[20];  
    int id;  
    float marks;  
};  
void main()  
{  
    struct student s1,s2,s3;  
    int dummy;  
    printf("Enter the name, id, and marks of student 1 ");  
    scanf("%s %d %f",s1.name,&s1.id,&s1.marks);  
    scanf("%c",&dummy);  
    printf("Enter the name, id, and marks of student 2 ");  
    scanf("%s %d %f",s2.name,&s2.id,&s2.marks);  
    scanf("%c",&dummy);  
    printf("Enter the name, id, and marks of student 3 ");  
    scanf("%s %d %f",s3.name,&s3.id,&s3.marks);  
    scanf("%c",&dummy);  
    printf("Printing the 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

Enter the name, id, and marks of student 1 Neha 90 90  
Enter the name, id, and marks of student 2 Ashok 90 90  
Enter the name, id, and marks of student 3 Priyank 90 90       
Printing the details....        
Neha 90 90.0000                          
Ashok 90 90.0000                      
Priyank 90 90.0000

Let’s see an example of an array of structures that stores information of 6 students

#include<stdio.h>  
#include <string.h>    
struct student{    
int rollno;    
char name[10];    
};    
int main(){    
int i;    
struct student st[6];    
printf("Enter Records of 6 students");    
for(i=0;i<6;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<6;i++){    
printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);    
}    
   return 0;    
}

Output

Enter Records of 5 students
Enter Rollno:1
Enter Name: Priyank
Enter Rollno:2
Enter Name:Rajul
Enter Rollno:3
Enter Name:Vaishali
Enter Rollno:4
Enter Name:Palak
Enter Rollno:5
Enter Name:Apoorv

Student Information List:
Rollno:1, Name:Priyank
Rollno:2, Name:Rajul
Rollno:3, Name:Vaishali
Rollno:4, Name:Palak
Rollno:5, Name:Apoorv

Submit your review
1
2
3
4
5
Submit
     
Cancel

Create your own review

The Technical Funda
Average rating:  
 0 reviews