Array of Structures in c programing with example
Nested structures in C programming - How to use an array of structures efficiently. #Array of Structures in c language with example
Meta Description: Learn how to use arrays of structures in C programming with practical examples. Discover the benefits of combining arrays and structures for efficient data management.
In C programming, an array is a data structure that allows you to store multiple values of the same data type under a single variable name. On the other hand, a structure is a user-defined data type that groups variables of different data types under a single name.
When you combine these two concepts, you get an array of structures, which is a powerful tool for managing complex data efficiently. This article will explain what an array of structures is, how to use it, and its advantages in C programming.
An array of structures is a collection of structure variables, where each element of the array is a structure. This allows you to store and manage multiple records (e.g., student details, employee records, etc.) in a single array.
For example, if you want to store the details of multiple students (name, age, and birthdate), you can use an array of structures instead of creating separate variables for each student.
Below is an example of how to declare, initialize, and use an array of structures in C:
#include <stdio.h>
#include <string.h>
// Outer structure for date
struct Date {
int day;
int month;
int year;
};
// Inner structure for student details
struct Student {
char name[50];
int age;
struct Date birthdate; // Nested structure
};
int main() {
// Declare an array of structures
struct Student studentArray[3];
// Assign values to the array of structures
studentArray[0] = (struct Student){"Developer Rahul", 25, {15, 1, 1998}};
studentArray[1] = (struct Student){"Developer Shubham", 30, {16, 1, 1990}};
studentArray[2] = (struct Student){"Developer Push", 28, {15, 4, 1994}};
// Access and print the values of the first student
printf("Name of student: %s\n", studentArray[0].name);
printf("Age: %d\n", studentArray[0].age);
printf("Birthdate: %d/%d/%d\n", studentArray[0].birthdate.day, studentArray[0].birthdate.month, studentArray[0].birthdate.year);
// Access and print the values of the second student
printf("Name of student: %s\n", studentArray[1].name);
printf("Age: %d\n", studentArray[1].age);
printf("Birthdate: %d/%d/%d\n", studentArray[1].birthdate.day, studentArray[1].birthdate.month, studentArray[1].birthdate.year);
// Access and print the values of the third student
printf("Name of student: %s\n", studentArray[2].name);
printf("Age: %d\n", studentArray[2].age);
printf("Birthdate: %d/%d/%d\n", studentArray[2].birthdate.day, studentArray[2].birthdate.month, studentArray[2].birthdate.year);
return 0;
}
struct Date
is used to store the birthdate (day, month, year).struct Student
contains the student's name, age, and a nested struct Date
for the birthdate.struct Student studentArray[3]
declares an array of structures to store details of 3 students.printf
function is used to access and display the values.An array of structures in C programming is a powerful way to manage and organize complex data. By combining the benefits of arrays and structures, you can create efficient, readable, and scalable programs. Whether you're working on student records, employee databases, or any other application, arrays of structures are a must-know concept for C programmers.