Friday, July 6, 2018

Array of Structures in C

There can be array of structures in C programming to store many information of different data types. The array of structures is also known as collection of structures.

Example:Let's see structure with array that stores information of 5 students and prints it.





#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:

Enter Records of 5 students
Enter Rollno:1
Enter Name:Rama
Enter Rollno:2
Enter Name:Pavi
Enter Rollno:3
Enter Name:Rama
Enter Rollno:4
Enter Name:Pavi
Enter Rollno:5
Enter Name:Rama


Student Information List:
Rollno:1, Name:Rama
Rollno:2, Name:Pavi
Rollno:3, Name:Rama
Rollno:4, Name:Pavi
Rollno:5, Name:Rama

As you know, C Structure is collection of different datatypes ( variables ) which are grouped together. Whereas, array of structures is nothing but collection of structures. This is also called as structure array in C.

EXAMPLE PROGRAM FOR ARRAY OF STRUCTURES IN C

This program is used to store and access “id, name and percentage” for 3 students. Structure array is used in this program to store and display records for many students. You can store “n” number of students record by declaring structure variable as ‘struct student record[n]“, where n can be 1000 or 5000 etc.





#include <stdio.h>
#include <string.h>

struct student
{
int id;
char name[30];
float percentage;
};

int main()
{
int i;
struct student record[2];

// 1st student's record
record[0].id=1;
strcpy(record[0].name, "Rama");
record[0].percentage = 86.5;

// 2nd student's record
record[1].id=2;
strcpy(record[1].name, "Pavi");
record[1].percentage = 90.5;

// 3rd student's record
record[2].id=3;
strcpy(record[2].name, "Rama");
record[2].percentage = 81.5;

for(i=0; i<3; i++)
{
printf(" Records of STUDENT : %d \n", i+1);
printf(" Id is: %d \n", record[i].id);
printf(" Name is: %s \n", record[i].name);
printf(" Percentage is: %f\n\n",record[i].percentage);
}
return 0;
}




OUTPUT:
Records of STUDENT : 1
Id is: 1
Name is: Rama
Percentage is: 86.500000

Records of STUDENT : 2
Id is: 2
Name is: Pavi
Percentage is: 90.500000

Records of STUDENT : 3
Id is: 3
Name is: Rama
Percentage is: 81.500000

EXAMPLE PROGRAM FOR DECLARING MANY STRUCTURE VARIABLE IN C

In this program, two structure variables “record1″ and “record2″ are declared for same structure and different values are assigned for both structure variables. Separate memory is allocated for both structure variables to store the data.





#include <stdio.h>
#include <string.h>

struct student
{
int id;
char name[30];
float percentage;
};

int main()
{
int i;
struct student record1 = {1, "Rama", 90.5};
struct student record2 = {2, "Pavi", 93.5};

printf("Records of STUDENT1: \n");
printf(" Id is: %d \n", record1.id);
printf(" Name is: %s \n", record1.name);
printf(" Percentage is: %f \n\n", record1.percentage);

printf("Records of STUDENT2: \n");
printf(" Id is: %d \n", record2.id);
printf(" Name is: %s \n", record2.name);
printf(" Percentage is: %f \n\n", record2.percentage);

return 0;
}




OUTPUT:
Records of STUDENT1:
Id is: 1
Name is: Rama
Percentage is: 90.500000
Records of STUDENT2:
Id is: 2
Name is: Pavi
Percentage is: 93.500000




Instagram



No comments:

Post a Comment