C Programs Tutorials | IT Developer
IT Developer

C Programming - C Arrays of Structures



Share with a Friend

C Programming - C Arrays of Structures

C Arrays of Structures

In C, an array of structures is a way to store multiple instances of a structure type in a contiguous block of memory. Each element in the array represents an individual structure, allowing you to manage and manipulate collections of related data efficiently.

Syntax for Declaring an Array of Structures

C

struct StructureName {

    data_type member1;

    data_type member2;

    // additional members

};

struct StructureName arrayName[array_size];

Example: Array of Structures

Defining and Using an Array of Structures

C

#include <stdio.h>

// Define a structure

struct Student {

    int id;

    char name[50];

    float marks;

};

int main() {

    // Declare an array of structures

    struct Student students[3];

    // Initialize the array of structures

    students[0].id = 101;

    students[0].marks = 85.5;

    snprintf(students[0].name, sizeof(students[0].name), "Alice");

    students[1].id = 102;

    students[1].marks = 78.0;

    snprintf(students[1].name, sizeof(students[1].name), "Bob");

    students[2].id = 103;

    students[2].marks = 92.3;

    snprintf(students[2].name, sizeof(students[2].name), "Charlie");

    // Display the data

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

        printf("Student %d:\n", i + 1);

        printf("ID: %d\n", students[i].id);

        printf("Name: %s\n", students[i].name);

        printf("Marks: %.2f\n\n", students[i].marks);

    }

    return 0;

}

Output:

Student 1:

ID: 101

Name: Alice

Marks: 85.50

Student 2:

ID: 102

Name: Bob

Marks: 78.00

Student 3:

ID: 103

Name: Charlie

Marks: 92.30

Initializing an Array of Structures

You can initialize an array of structures at the time of declaration.

Example:

C

#include <stdio.h>

struct Point {

    int x;

    int y;

};

int main() {

    // Array of structures with initialization

    struct Point points[3] = {

        {10, 20},

        {30, 40},

        {50, 60}

    };

    // Accessing and printing array elements

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

        printf("Point %d: (%d, %d)\n", i + 1, points[i].x, points[i].y);

    }

    return 0;

}

Output:

Point 1: (10, 20)

Point 2: (30, 40)

Point 3: (50, 60)

Passing an Array of Structures to a Function

An array of structures can be passed to a function for processing.

Example:

C

#include <stdio.h>

struct Employee {

    int id;

    char name[50];

    float salary;

};

void displayEmployees(struct Employee employees[], int size) {

    for (int i = 0; i < size; i++) {

        printf("Employee %d:\n", i + 1);

        printf("ID: %d\n", employees[i].id);

        printf("Name: %s\n", employees[i].name);

        printf("Salary: %.2f\n\n", employees[i].salary);

    }

}

int main() {

    struct Employee employees[2] = {

        {1, "Alice", 50000},

        {2, "Bob", 60000}

    };

    displayEmployees(employees, 2);

    return 0;

}

Output:

Employee 1:

ID: 1

Name: Alice

Salary: 50000.00

Employee 2:

ID: 2

Name: Bob

Salary: 60000.00

Use Cases of Arrays of Structures

  1. Managing Student Records: Store and process data for multiple students.
  2. Employee Management Systems: Track employee details like ID, salary, and name.
  3. Inventory Management: Maintain product details in a shop or warehouse.
  4. Geographical Points: Store coordinates for graphical or mapping applications.

Advantages

  1. Efficient Data Management: Combines similar types of data in one place.
  2. Simplifies Code: Access elements using indices, reducing complexity.
  3. Easy to Pass: Entire arrays can be passed to functions for processing.

Conclusion

An array of structures in C provides a robust way to handle collections of related data. It allows programmers to organize and manipulate large datasets efficiently, improving modularity and maintainability of the code.