In C programming, a structure is used to group related data of different types. Sometimes, we need to store data for multiple similar entities, such as multiple students or employees. In such cases, combining structures with arrays allows us to manage multiple records efficiently.

Array of Structures in C Programming

What is Array of Structures?

An Array of Structures is a collection of structure variables stored in a contiguous memory block. Using arrays with structures helps us avoid declaring multiple separate structure variables. It makes code cleaner and easier to manage.

Syntax for Array of Structures

struct structure_name structure_variable_name[array_size];

Example:

struct Employee info[3];

Here, info is an array of 3 Employee structures.

Example: Storing Student Marks

Suppose we want to store marks of 4 students in 3 subjects. Using an array of structures:

struct marks

{

int sub1;

int sub2;

int sub3;

};

int main()

{

static struct marks Student[4] = {

{41, 12, 22},

{52, 55, 66},

{32, 55, 68},

{55, 44, 88}

};

return 0;

}

This efficiently stores data for multiple students without declaring separate variables for each student.

Example Without Using Array

#include <stdio.h>

#include <string.h>

struct Employee

{

int emp_id;

char emp_name[20];

float salary;

};

int main()

{

struct Employee info1, info2, info3;

info1.emp_id = 34;

strcpy(info1.emp_name, “Raj”);

info1.salary = 20000.00;

info2.emp_id = 35;

strcpy(info2.emp_name, “Maruti”);

info2.salary = 30000.00;

info3.emp_id = 36;

strcpy(info3.emp_name, “Suraj”);

info3.salary = 40000.00;

printf(“Employee 1\nID: %d\nName: %s\nSalary: %.2f\n\n”, info1.emp_id, info1.emp_name, info1.salary);

printf(“Employee 2\nID: %d\nName: %s\nSalary: %.2f\n\n”, info2.emp_id, info2.emp_name, info2.salary);

printf(“Employee 3\nID: %d\nName: %s\nSalary: %.2f\n\n”, info3.emp_id, info3.emp_name, info3.salary);

return 0;

}

Output:

Employee 1

ID: 34

Name: Raj

Salary: 20000.00

Employee 2

ID: 35

Name: Maruti

Salary: 30000.00

Employee 3

ID: 36

Name: Suraj

Salary: 40000.00

Example Using Array of Structures

#include <stdio.h>

#include <string.h>

struct Employee

{

int emp_id;

char emp_name[20];

float salary;

};

int main()

{

struct Employee info[3];

int i;

info[0].emp_id = 34;

strcpy(info[0].emp_name, “Raj”);

info[0].salary = 20000.00;

info[1].emp_id = 35;

strcpy(info[1].emp_name, “Maruti”);

info[1].salary = 30000.00;

info[2].emp_id = 36;

strcpy(info[2].emp_name, “Suraj”);

info[2].salary = 40000.00;

for(i = 0; i < 3; i++)

{

printf(“Employee %d\n”, i+1);

printf(“ID: %d\n”, info[i].emp_id);

printf(“Name: %s\n”, info[i].emp_name);

printf(“Salary: %.2f\n\n”, info[i].salary);

}

return 0;

}

Output:

Employee 1

ID: 34

Name: Raj

Salary: 20000.00

Employee 2

ID: 35

Name: Maruti

Salary: 30000.00

Employee 3

ID: 36

Name: Suraj

Salary: 40000.00

Advantages of Using Array of Structures

  1. Reduces the need to declare multiple individual variables.
  2. Makes the program cleaner and easier to manage.
  3. Allows easy traversal of records using loops.
  4. Supports combining multiple data types into a single unit.

Conclusion

Using arrays of structures in C programming is a powerful way to manage multiple records efficiently. It simplifies data handling, reduces redundancy, and improves program readability.

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *