C arrays of structures


Arrays of structures in C allow you to create a collection of structure variables. This is useful for managing multiple records of the same type, such as a list of students, products, or any other entities that share the same attributes.

Defining a Structure

Before you can create an array of structures, you first need to define a structure. For example, let's define a structure called Person to hold information about individuals:

struct Person { char name[50]; int age; float height; };

Declaring an Array of Structures

Once you have defined the structure, you can declare an array of that structure type. Here's how to create an array that can hold multiple Person structures:

struct Person people[3]; // Array of 3 Person structures

Initializing the Array of Structures

You can initialize the array of structures using an initializer list. Here's how to initialize an array with some data:

struct Person people[3] = { {"Alice", 25, 5.7}, {"Bob", 30, 6.1}, {"Charlie", 28, 5.9} };

Accessing Members of Array of Structures

To access the members of a specific structure in the array, use the dot (.) operator in conjunction with the array subscript. Here’s how you can print the information of each person in the array:

for (int i = 0; i < 3; i++) { printf("Name: %s, Age: %d, Height: %.1f\n", people[i].name, people[i].age, people[i].height); }

Example Program Using an Array of Structures

Here’s a complete example that demonstrates defining a structure, declaring an array of structures, initializing it, and accessing its members:

#include <stdio.h> #include <string.h> struct Person { char name[50]; int age; float height; }; int main() { // Declare and initialize an array of structures struct Person people[3] = { {"Alice", 25, 5.7}, {"Bob", 30, 6.1}, {"Charlie", 28, 5.9} }; // Accessing and printing the members of each structure for (int i = 0; i < 3; i++) { printf("Person %d: Name: %s, Age: %d, Height: %.1f\n", i + 1, people[i].name, people[i].age, people[i].height); } return 0; }

Output

When you run this program, the output will be:

Person 1: Name: Alice, Age: 25, Height: 5.7 Person 2: Name: Bob, Age: 30, Height: 6.1 Person 3: Name: Charlie, Age: 28, Height: 5.9

Modifying Array Elements

You can also modify the members of the structures in the array after initialization. For example, to change Bob’s age:

people[1].age = 31; // Change Bob's age to 31

Passing Arrays of Structures to Functions

You can pass an array of structures to functions. When passing the array to a function, you can simply pass the name of the array:

void displayPeople(struct Person p[], int size) { for (int i = 0; i < size; i++) { printf("Name: %s, Age: %d, Height: %.1f\n", p[i].name, p[i].age, p[i].height); } } int main() { struct Person people[3] = { {"Alice", 25, 5.7}, {"Bob", 30, 6.1}, {"Charlie", 28, 5.9} }; displayPeople(people, 3); // Pass the array to the function return 0; }

Summary

  • Arrays of structures allow you to manage multiple records of the same type efficiently.
  • Use the dot (.) operator to access members of each structure in the array.
  • You can initialize the array at declaration or assign values later.
  • Arrays of structures can be passed to functions, making it easier to manipulate collections of related data.

Arrays of structures are a powerful feature in C that facilitate the organization and manipulation of grouped data, particularly when dealing with multiple records of the same type.