๐Ÿงฑ C Structures, Unions & Enums
Estimated reading: 3 minutes 7 views

๐Ÿงฐ C Arrays of Structures โ€“ Manage Multiple Records Easily


๐Ÿงฒ Introduction โ€“ What Are Arrays of Structures in C?

In C programming, an array of structures is a collection of multiple struct variables stored in contiguous memory locations. Each element of the array is a full structure instance, making this approach ideal for managing records like students, employees, products, and more.

๐ŸŽฏ In this guide, youโ€™ll learn:

  • How to declare and initialize an array of structures
  • Access and manipulate individual structure elements
  • Use arrays of structs in functions
  • Real-world use cases and best practices

๐Ÿ” Core Concept โ€“ Why Use Arrays of Structures?

When you need to manage multiple records of the same type, like 100 students or 50 books, using an array of structures allows:

  • Organized access to each record
  • Grouping related attributes together
  • Efficient memory management and iteration

๐Ÿ’ป Code Examples โ€“ Declaring and Using Struct Arrays

โœ… Example 1: Basic Declaration and Initialization

#include <stdio.h>

struct Student {
    char name[50];
    int rollNo;
    float marks;
};

int main() {
    struct Student students[3] = {
        {"Alice", 1, 85.5},
        {"Bob", 2, 90.0},
        {"Charlie", 3, 78.0}
    };

    for (int i = 0; i < 3; i++) {
        printf("Name: %s, Roll: %d, Marks: %.2f\n",
               students[i].name, students[i].rollNo, students[i].marks);
    }

    return 0;
}

โœ… Example 2: Modifying Values in Struct Array

#include <stdio.h>

struct Product {
    char name[30];
    float price;
};

int main() {
    struct Product items[2];

    // Assigning values
    items[0].price = 99.99;
    strcpy(items[0].name, "Mouse");

    items[1].price = 149.99;
    strcpy(items[1].name, "Keyboard");

    for (int i = 0; i < 2; i++) {
        printf("Product: %s, Price: $%.2f\n", items[i].name, items[i].price);
    }

    return 0;
}

๐Ÿ“Œ You can access members using dot syntax: items[i].member.


๐Ÿ“š Use Cases for Struct Arrays

ApplicationUse Case
Education SystemsStudent records, exam results
Inventory SystemsProduct listings, prices, stock counts
Game DevelopmentEnemy/player stats, levels, scores
Payroll/HREmployee details, salary records

๐Ÿ”„ Passing Struct Arrays to Functions

void printStudents(struct Student s[], int count) {
    for (int i = 0; i < count; i++) {
        printf("%s - %.2f\n", s[i].name, s[i].marks);
    }
}

๐Ÿง  Arrays are passed as pointers, so changes in the function reflect outside as well.


๐Ÿ’ก Best Practices & Tips

๐Ÿ“˜ Best Practice:
Use loops and helper functions to manipulate large arrays of structures cleanly.

๐Ÿ’ก Tip:
Use typedef to simplify structure array declarations:

typedef struct {
    char name[50];
    float price;
} Product;

Product inventory[100];

โš ๏ธ Pitfall:
Accessing uninitialized structure members may lead to undefined behavior. Always initialize your data.


๐Ÿ› ๏ธ Real-World Applications

  • ๐Ÿ“‹ Student record management systems
  • ๐Ÿ›’ Inventory and billing software
  • ๐Ÿง  Game entity systems
  • ๐Ÿ“Š Tabular data in CLI tools

๐Ÿ“Œ Summary โ€“ Recap & Next Steps

Arrays of structures make it easy to manage collections of complex data. They combine the benefits of structured data and indexed access, allowing you to build scalable, organized applications.

๐Ÿ” Key Takeaways:

  • Array of structs = multiple grouped records
  • Access via arr[i].member
  • Can be passed to functions and manipulated efficiently
  • Excellent for real-world data modeling and storage

โš™๏ธ Real-World Relevance:

Used in databases, forms, billing systems, games, and IoT firmware for managing sensor or user data.


โ“ Frequently Asked Questions (FAQ)

โ“ What is an array of structures in C?

โœ… It’s a fixed-size list of struct variables. Each element stores a group of related data types.


โ“ How do I access structure members in an array?

โœ… Use the syntax: arr[i].memberName


โ“ Can I pass an array of structs to a function?

โœ… Yes. You can pass it as an argument using the struct array name.


โ“ Can structs contain arrays inside them?

โœ… Yes. Structures can have arrays as members (e.g., char name[50];).


โ“ Can I use malloc() for dynamic arrays of structs?

โœ… Yes:

Student *students = malloc(sizeof(Student) * count);

Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿงฐ C Arrays of Structures

Or Copy Link

CONTENTS
Scroll to Top