Table of Contents
show
Structure is used to store the information of one particular object but if we need to store such 100 objects then Array of Structure is used.
Two ways to declare array of structure,
Method 1
struct student
{
char name[20];
int roll_no;
float marks;
}arr_student[2];
Method 2
struct student
{
char name[20];
int roll_no;
float marks;
};
stuct student arr_student[2];
Example: Displaying student information using array of structures
#include<stdio.h>
#include<string.h>
#define MAX 2
struct student
{
char name[20];
int roll_no;
float marks;
};
int main()
{
struct student arr_student[MAX];
int i;
for(i = 0; i < MAX; i++ )
{
printf("\nEnter details of student %d\n\n", i+1);
printf("Enter name: ");
scanf("%s", arr_student[i].name);
printf("Enter roll no: ");
scanf("%d", &arr_student[i].roll_no);
printf("Enter marks: ");
scanf("%f", &arr_student[i].marks);
}
printf("\n");
printf("Name\tRoll no\tMarks\n");
for(i = 0; i < MAX; i++ )
{
printf("%s\t%d\t%.2f\n",
arr_student[i].name, arr_student[i].roll_no, arr_student[i].marks);
}
return 0;
}
Output
C:\TDM-GCC-64>gcc 4array_Struct1.c -o 4as
C:\TDM-GCC-64>4as
Enter details of student 1
Enter name: ABC
Enter roll no: 101
Enter marks: 90
Enter details of student 2
Enter name: QWE
Enter roll no: 202
Enter marks: 90
Name Roll no Marks
ABC 101 90.00
QWE 202 90.00
Example: Displaying coordinates of 6 points using array of structures
#include<stdio.h>
struct point
{
int x;
int y;
}p[6];
int main()
{
int i;
for (i=0;i<6;i++)
{
printf("Enter the value of object %d",i);
scanf("%d%d",&p[i].x,&p[i].y);
}
printf("object_no \t x \t y \n");
for (i=0;i<6;i++)
{
printf("%d ",i);
printf("%d\t%d",p[i].x,p[i].y);
printf("\n");
}
}
Output
C:\TDM-GCC-64>gcc 4_array_St.c -o 4ast
C:\TDM-GCC-64>4ast
Enter the value of object 0 3
4
Enter the value of object 1 5
6
Enter the value of object 2 8
9
Enter the value of object 3 1
2
Enter the value of object 4 4
5
Enter the value of object 5 8
9
object_no x y
0 3 4
1 5 6
2 8 9
3 1 2
4 4 5
5 8 9
Example: Displaying employee information using array of structures
#include<stdio.h>
struct employee
{
char name[10];
int no;
int bpay,hra,da,cca,pf;
float npay,gpay;
int date,month,year;
}e[10];
int main()
{
int i,n,eno,flag = -1;
printf("Enter number of employees\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter employee number:\n");
scanf("%d",&e[i].no);
printf("Enter name:\n");
scanf("%s",e[i].name);
printf("Enter date of joining");
scanf("%d%d%d",&e[i].date,&e[i].month,&e[i].year);
printf("Enter bpay,hra,da,cca,pf\n");
scanf("%d%d%d%d%d",&e[i].bpay,&e[i].hra,&e[i].da,&e[i].cca,&e[i].pf);
e[i].gpay =e[i].bpay+(e[i].bpay*e[i].da)/100+(e[i].bpay*e[i].hra)/100+(e[i].bpay*e[i].cca)/100;
e[i].npay=e[i].gpay-(e[i].bpay*e[i].pf)/100;
}
for(i=0;i<n;i++)
{
printf("Employee number:%d\n",e[i].no);
printf("Name: %s \n",e[i].name);
printf("Date of Joining:%d-%d-%d\n",e[i].date,e[i].month,e[i].year);
printf("Basic Pay:%d\n",e[i].bpay);
printf("DA:%d\n",e[i].da);
printf("HRA:%d\n",e[i].hra);
printf("CCA:%d\n",e[i].cca);
printf("PF:%d\n",e[i].pf);
printf("Gross pay:%f\n",e[i].gpay);
printf("Net pay:%f\n",e[i].npay);
}
//Searching
printf("Enter employee number to search\n");
scanf("%d",&eno);
for(i=0;i<n;i++)
{
if(eno == e[i].no)
{
flag = 1;
break;
}
}
if(flag == 1)
{
printf("Employee details are present\n");
printf("Employee number:%d\n",e[i].no);
printf("Name: %s \n",e[i].name);
printf("Date of Joining:%d-%d-%d\n",e[i].date,e[i].month,e[i].year);
printf("Basic Pay:%d\n",e[i].bpay);
printf("DA:%d\n",e[i].da);
printf("HRA:%d\n",e[i].hra);
printf("CCA:%d\n",e[i].cca);
printf("PF:%d\n",e[i].pf);
printf("Gross pay:%f\n",e[i].gpay);
printf("Net pay:%f\n",e[i].npay);
}
else
{
printf("No such employee");
}
return 0;
}
Output
C:\TDM-GCC-64>gcc 4_Employee_array_Struct.c -o 4EaS
C:\TDM-GCC-64>4EaS
Enter number of employees
2
Enter employee number:
101
Enter name:
abc
Enter date of joining 23
2
2021
Enter bpay,hra,da,cca,pf
5000
1000
2000
10
10
Enter employee number:
202
Enter name:
qwe
Enter date of joining 21
1
2020
Enter bpay,hra,da,cca,pf
6000
1000
10
10
10
Employee number:101
Name: abc
Date of Joining:23-2-2021
Basic Pay:5000
DA:2000
HRA:1000
CCA:10
PF:10
Gross pay:155500.000000
Net pay:155000.000000
Employee number:202
Name: qwe
Date of Joining:21-1-2020
Basic Pay:6000
DA:10
HRA:1000
CCA:10
PF:10
Gross pay:67200.000000
Net pay:66600.000000
Enter employee number to search
203
No such employee
C:\TDM-GCC-64>
Views: 0