Introduction:
In the world of C programming, arrays of structures provide a powerful mechanism for managing and organizing related data. They allow us to create collections of custom data types, making it easier to work with multiple records in a systematic way. In this blog post, we’ll explore the concept of arrays of structures, covering their definition, initialization, accessing elements, and practical examples to illustrate their utility.

Understanding Arrays of Structures:

  1. Definition of Array of Structures:
   struct Student {
       int rollNumber;
       char name[50];
       float marks;
   };

   struct Student class[5]; // Array of 5 Student structures
  1. Declaration and Initialization:
   struct Student class[3] = {
       {101, "Alice", 85.5},
       {102, "Bob", 90.0},
       {103, "Charlie", 78.3}
   };

Accessing Elements of Array of Structures:

  1. Using Indexing:
   printf("Student 1 - Roll Number: %d\n", class[0].rollNumber);
   printf("Student 2 - Name: %s\n", class[1].name);
   printf("Student 3 - Marks: %.2f\n", class[2].marks);
  1. Looping Through Array:
   for (int i = 0; i < 3; i++) {
       printf("Student %d - Roll Number: %d\n", i + 1, class[i].rollNumber);
       printf("Student %d - Name: %s\n", i + 1, class[i].name);
       printf("Student %d - Marks: %.2f\n", i + 1, class[i].marks);
       printf("------------------------\n");
   }

Passing Array of Structures to Functions:

  1. Passing Entire Array:
   void displayStudents(struct Student arr[], int size) {
       for (int i = 0; i < size; i++) {
           printf("Roll Number: %d\n", arr[i].rollNumber);
           printf("Name: %s\n", arr[i].name);
           printf("Marks: %.2f\n", arr[i].marks);
           printf("------------------------\n");
       }
   }

   displayStudents(class, 3); // Passing array 'class' to function
  1. Modifying Array Elements:
   void updateMarks(struct Student arr[], int size, int roll, float newMarks) {
       for (int i = 0; i < size; i++) {
           if (arr[i].rollNumber == roll) {
               arr[i].marks = newMarks;
               printf("Marks updated for Roll Number %d\n", roll);
               return;
           }
       }
       printf("Roll Number %d not found\n", roll);
   }

   updateMarks(class, 3, 102, 95.5); // Update marks for Roll Number 102

Practical Example: Employee Records

Let’s create a practical example of an array of structures to manage employee records.

#include <stdio.h>
#include <string.h>

struct Employee {
    int empId;
    char name[50];
    float salary;
};

void displayEmployees(struct Employee arr[], int size) {
    for (int i = 0; i < size; i++) {
        printf("Employee ID: %d\n", arr[i].empId);
        printf("Name: %s\n", arr[i].name);
        printf("Salary: %.2f\n", arr[i].salary);
        printf("------------------------\n");
    }
}

int main() {
    struct Employee employees[3] = {
        {101, "Alice", 50000.0},
        {102, "Bob", 60000.0},
        {103, "Charlie", 55000.0}
    };

    printf("Initial Employee Records:\n");
    displayEmployees(employees, 3);

    // Update salary for employee with ID 102
    for (int i = 0; i < 3; i++) {
        if (employees[i].empId == 102) {
            employees[i].salary = 65000.0;
            break;
        }
    }

    printf("\nAfter Salary Update:\n");
    displayEmployees(employees, 3);

    return 0;
}

Conclusion:
Arrays of structures in C provide a powerful way to organize and manage related data efficiently. They allow us to create collections of custom data types, making it easier to work with multiple records in a systematic manner. By understanding how to define, declare, initialize, access, and pass arrays of structures to functions, we can build robust and scalable applications. Experiment with different scenarios, explore additional functionalities, and apply these concepts to your programming projects. Arrays of structures are a cornerstone of C programming, and mastering them will enable you to create versatile and efficient applications.

Leave a Reply