Friday 12 January 2018

Array of Structures in C with Example program

So for in these series we have seen how to create structure, how to access structure members, nested structures ext. A single structure is capable of handling only one person data. Suppose imagine in your class / company more than one student / employees and you need to handle all details of all persons. Then you need to create a single structure for each and every one. This is some times good. But what if the members are more than 50? more than 500? Is creating individual structures for all of the is possible?. Yes, But it takes lot of time to write program, read values, and process structure members. The solution is to create an array of structure.

In C, it is possible  to create a structure variable as an array of type structure, such structure is refred as array of structure.Array of structure can be used to handle multiple data items at a time. In a class, we do not have just one student, but there may be at least 30 students. So, the same definition of structure can be used for all the 30 students. This could be possible when we make an array of structure. An array of structure is declared in the same way as we had declared an array of built in data types.

Here is an example, where I have create an array of structure to read data for 50 students with same definition of structure.

struct student
{
int rno;
char fee[80];
float fee;
char dob[80];
}; 

void main()
{
struct student s[50];          // structure with 50 members
int n,i;
clrscr();
printf(" \n enter the student details:");
for(i=0;i<50 i="" p="">
{
printf(" \n enter the  roll number:");
scanf(" %d", &s[i].rno):
printf(" \n enter the  name:");
gets(s[i].name);
printf(" \n enter the  fee:");
scanf("%f", &s[i].fee);
printf(" \n enter the  DOB:");
gets(s[i].dob);
}
printf(" \n you have enter the following details:"); 
for(i=0;i<50 i="" p="">
{
printf(" \n the  roll number=%d:"s[i].rno);
printf(" \n the  name=%s:"s[i].name);
printf(" \n the  Fee=%f:"s[i].fee);
printf(" \n the  DOB=%s:"s[i].dob);
}
getch();

0 comments :

Post a Comment

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top