Array of Structures in C
An array of structures allows you to store multiple instances of a structure in a single array. This is particularly useful when you need to manage and manipulate collections of data that follow the same structure.
At SamagraCS Educational Technology, we believe in making concepts easy and intuitive. Let’s dive into the concept of arrays of structures and explore how it simplifies handling multiple records in C.
What is an Array of Structures?
An array of structures is simply an array where each element is a structure. This allows you to store and access multiple instances of the structure efficiently, just like handling an array of primitive data types (like int
, float
, etc.), but now with more complex data.
Real-Life Analogy:
Imagine you are a school administrator managing student records. Instead of creating separate variables for each student, you can use an array of structures, where each element in the array represents a student and holds their data (name, age, marks, etc.).
Defining an Array of Structures
You can define an array of structures in C by first defining the structure and then declaring an array of that structure type.
Syntax:
struct structure_name {
data_type1 variable1;
data_type2 variable2;
// More variables
};
struct structure_name array_name[array_size];
- structure_name: Name of the structure.
- array_name: Name of the array that will hold multiple structures.
- array_size: Number of elements (structures) in the array.
Example: Student Records Using Array of Structures
Let’s create an array of structures to store information about multiple students.
#include <stdio.h>
#include <string.h>
struct Student {
char name[50];
int age;
float marks;
};
int main() {
// Array of structures to store information about 3 students
struct Student students[3];
// Assigning values to the first student
strcpy(students[0].name, "Pawan Jaiswal");
students[0].age = 20;
students[0].marks = 85.5;
// Assigning values to the second student
strcpy(students[1].name, "Pooja Jaiswal");
students[1].age = 19;
students[1].marks = 90.3;
// Assigning values to the third student
strcpy(students[2].name, "John Doe");
students[2].age = 22;
students[2].marks = 78.0;
// Printing student information
for (int i = 0; i < 3; i++) {
printf("Student %d Name: %s\n", i + 1, students[i].name);
printf("Student %d Age: %d\n", i + 1, students[i].age);
printf("Student %d Marks: %.2f\n", i + 1, students[i].marks);
printf("\n");
}
return 0;
}
Output:
Student 1 Name: Pawan Jaiswal
Student 1 Age: 20
Student 1 Marks: 85.50
Student 2 Name: Pooja Jaiswal
Student 2 Age: 19
Student 2 Marks: 90.30
Student 3 Name: John Doe
Student 3 Age: 22
Student 3 Marks: 78.00
Explanation of the Example
- Structure Definition:
- We define a structure named
Student
to store information like the student’s name, age, and marks.
- Array of Structures:
- We declare an array
students[3]
to store information about 3 students. Each element of the array is aStudent
structure.
- Accessing and Modifying Structure Members:
- To access or modify individual members of each structure in the array, we use the dot operator (
.
), combined with the array index. For example:c students[0].name // Access the name of the first student students[1].age // Access the age of the second student students[2].marks // Access the marks of the third student
- Looping through the Array:
- We use a
for
loop to iterate over the array and print the details of each student.
Real-Life Example: Employee Records Using Array of Structures
Now, let’s create an array of structures to store information about multiple employees.
Code Example:
#include <stdio.h>
#include <string.h>
struct Employee {
int id;
char name[50];
float salary;
};
int main() {
// Array of structures to store information about 2 employees
struct Employee employees[2];
// Assigning values to the first employee
employees[0].id = 101;
strcpy(employees[0].name, "Pawan Jaiswal");
employees[0].salary = 60000.75;
// Assigning values to the second employee
employees[1].id = 102;
strcpy(employees[1].name, "Pooja Jaiswal");
employees[1].salary = 70000.85;
// Printing employee information
for (int i = 0; i < 2; i++) {
printf("Employee %d ID: %d\n", i + 1, employees[i].id);
printf("Employee %d Name: %s\n", i + 1, employees[i].name);
printf("Employee %d Salary: %.2f\n", i + 1, employees[i].salary);
printf("\n");
}
return 0;
}
Output:
Employee 1 ID: 101
Employee 1 Name: Pawan Jaiswal
Employee 1 Salary: 60000.75
Employee 2 ID: 102
Employee 2 Name: Pooja Jaiswal
Employee 2 Salary: 70000.85
Accessing Structure Members in an Array of Structures
You can access members of a structure in an array using the following syntax:
array_name[index].member_name
For example:
students[0].name
accesses thename
of the first student.employees[1].salary
accesses thesalary
of the second employee.
Key Points to Remember
- Organized Data: Arrays of structures allow you to organize and store multiple records, each containing several fields, in a structured and accessible way.
- Efficient Access: You can use array indexing to access specific records and modify them easily.
- Scalability: Arrays of structures are ideal when handling multiple records, such as student details, employee information, or product listings.
An array of structures is a powerful tool for handling multiple records in C programming. It enables you to group related data together and efficiently manage a collection of data items. At SamagraCS Educational Technology, we encourage students to practice working with arrays of structures to gain a deeper understanding of data management in C.
For any questions or further assistance, feel free to reach out to Pawan & Pooja , or the team at SamagraCS Educational Technology. Keep practicing, and happy coding!