Array of Structure: Array of structure in C Language is the collection of multiple structure variables contains information about the values different data types.The collection of structure in c is a known as the array of structure.
Array of Structure Structure in C Programming.
Let us assume that we are going to develope a software for a company y to manage all employees information.
Employees has employee number, employee salary, employee name,employee department number,employee job and so on.
To store such kind of information we did use structure .
what we can use if you have to store information for more than one employees?
The solution for the given problem is array of structure .
we can use structure to store this kind of imformation .However the complexity of the program will increase if there are 10 employees.In this case we have to declare 10 different structure variable and store them one by one.This process will be tough since we have to declare a variable every time to add new employee.To manage and remember the structure variable name is very tricky task.
To avoid this C language enables us to declare an array of structure.Array of structure mean collection of all the structures that store the information of differnt entities.
Let understand the array of structure pictorialy from the given figure.
struct employee
{
int empno;
char ename[5];
int salary;
};
struct employee e[2];
we declared the structure and have created the struct variable e[2].
The array of structure is created i.e e[2].
The array of structure e[2] store the information of two different employees.
Details of first employees will be stored at e[0]the first index position of the array.
and the second employees will be at e[1].It is a contiguous allocation of memory for composite types.
The total memory allocation for array of structure from the given fig. is 26 bytes.
Note: Some time the allocation of bytes depends on the system architecture.
Advantage of Array of Structure in C programming.
The advantage of an array of structure in C language is
1. With the help of an array user can reprsent multiple values with a single array Variable name.
2. The array of structure improves code reusability and readability is also increased.
3. Multiple structure variables or redudent variables can be stored in single variable.
e.g. No need to remember structure variables like:
struct employee e1, e2, e3;
Instead, use an array of structures like:
struct employee e[3];
4. An array of structure contains all the structures variables that store the data of separate entities or structure variables.
struct employee
{
int empno;
char ename[30];
char job[30];
int salary;
float bonus;
int deptno;
};
struct employee e[2];
Array of Structure Example in C language.
Create array of structure to take input of many employees and display the employees information.
#include<stdio.h>
struct employee
{
int empno;
char name[20];
float salary;
};
int main()
{
struct employee e[2];
for (int i = 0; i < 2; i++)
{
printf("\nEnter details of employee %d\n", i + 1);
printf("\nEnter emp no: ");
scanf("%d", &e[i].empno);
printf("Enter name: ");
scanf("%s", e[i].name);
printf("\nEnter salary: ");
scanf("%f", &e[i].salary);
}
printf("\nEmployee Information\n");
printf("Empno\t\tName\t\t\tSalary\n");
for (int i = 0; i < 2; i++)
{
printf("%d\t\t%s\t\t\t%.2f \n",
s[i].empno, s[i].name, s[i].salary);
}
return 0;
}
Output:
Enter details of employee 1:
Enter emp no: 101
Enter name: Ajay
Enter salary:34500
Enter details of employee 2:
Enter emp no: 102
Enter name: Kajol
Enter salary: 34500.
Employee Information
empno Name salary.
101 Ajay 34500
102 Kajo; 34500
Previous Topic:-->> Copy and Compare structurex || Next topic:-->>Array within Structure.
Other Topics:
Variables and Identifiers
Relational Operators
if-else statements
Switch case
While Loop
Infinite while Loops
C FOR Loop
Infinite for Loops
Continue in Loops
One Dimensional Array
Two Dimensional Arrays
Read and Display 2D Arrays
Types of functions
Passing Array To Functions
Nesting of Function
Array vs Structure
Array of Structure
Structures and Functions
Structures Within Structures
Use Of Pointers In C
File Handling In C
Loops FAQ
Arrays FAQ
count vowels in a file
Function FAQ
Conditional Statements Assignments
For Loops Assignments
Arrays Assignments
Function Assignments
Structure Assignments
Pointers Assignments
Files Assignments
Storage classes Assignments
Binary Files
count words,lines in a file
Copy files
Update File
Continue in Loops
break in Loops
Difference Between While and Do while
difference while do..while & for
malloc
calloc
Storage Classes
Operators MCQ
Conditional Statements MCQ
Loops MCQ
Arrays MCQ
Function MCQ
Structure MCQ
Pointers MCQ
Files MCQ
Storage classes MCQ