explain array of structures with the help of a suitable example
Answers
Answered by
6
Array of Structures
In programming, structure is a composite datatype with a collection of variables. These variables can have different data types and collectively form a structure of a composite datatype. An array of structures is a sequential collection of structures. With structures, you can store mixed record types, and with an array supporting this, you can have a list of mixed record types. The following example shows a structure called student that takes the roll number and name of a student as an input, then stores each record in an array st, which stores three elements. Each element will hold a mixed record.
Here some examples:
#include<stdio.h>
#include <string.h>
struct student{
int rollno;
char name[10];
};
int main(){
int i;
struct student st[3];
printf("Enter Records of 3 students");
for(i=0;i<3;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<3;i++){ printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);
}
return 0;
}
Enter Records of 3 students
Enter Rollno:1
Enter Name:Jenny
Enter Rollno:2
Enter Name:Angela
Enter Rollno:3
Enter Name:Rory
Student Information List:
Rollno:1, Name: Jenny
Rollno:2, Name: Angela
Rollno:3, Name: Rory
In programming, structure is a composite datatype with a collection of variables. These variables can have different data types and collectively form a structure of a composite datatype. An array of structures is a sequential collection of structures. With structures, you can store mixed record types, and with an array supporting this, you can have a list of mixed record types. The following example shows a structure called student that takes the roll number and name of a student as an input, then stores each record in an array st, which stores three elements. Each element will hold a mixed record.
Here some examples:
#include<stdio.h>
#include <string.h>
struct student{
int rollno;
char name[10];
};
int main(){
int i;
struct student st[3];
printf("Enter Records of 3 students");
for(i=0;i<3;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<3;i++){ printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);
}
return 0;
}
Enter Records of 3 students
Enter Rollno:1
Enter Name:Jenny
Enter Rollno:2
Enter Name:Angela
Enter Rollno:3
Enter Name:Rory
Student Information List:
Rollno:1, Name: Jenny
Rollno:2, Name: Angela
Rollno:3, Name: Rory
Answered by
2
array is the combination of similar type of data .generally when we initialise similar element we can't initialise it without array
of many value.array consist of base address position address etc
hope it helps you
of many value.array consist of base address position address etc
hope it helps you
Attachments:
Similar questions