Array of Structures in C: Complete Guide with Examples & Use Cases

In C programming, structures let you group related variables under a single data type. But what if you want to store and manage multiple records of similar type, such as students, employees, or devices?

That’s where array of structures comes in. You get the best of both worlds:

This guide will help you:


🧩 Basic Structure Example in C

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

This structure defines a student record. It contains an integer roll, a string name, and a float marks. Now let’s see how we can store multiple such records using an array.


📦 Declaring an Array of Structures

struct Student class[3];

This line declares an array named class which can hold 3 Student records. Each element (e.g., class[0]) is a complete structure with roll, name, and marks.


✅ Initializing Array of Structures (Compile-Time)

struct Student class[3] = {
    {1, "Amit", 85.0},
    {2, "Neha", 91.5},
    {3, "Rahul", 76.2}
};

This initializes all 3 records in the array using curly braces. Each entry in the array has all the structure members filled with data. It’s neat, readable, and avoids runtime assignment.


🧑‍💻 Accessing Structure Members

printf("Name of second student: %s\n", class[1].name);

You can use dot (.) operator with the array index to access any field. Here, class[1].name refers to the name of the second student, which will output “Neha”.


🔄 Using Loop to Access All Records

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

This loop iterates over the array and prints the fields of each student. The use of index i allows accessing each student structure sequentially. This is especially useful when the array size is large or dynamic.


📝 Taking Input for Array of Structures (Runtime)

for (int i = 0; i < 2; i++) {
    printf("Enter Roll, Name, and Marks for student %d: ", i + 1);
    scanf("%d %s %f", &class[i].roll, class[i].name, &class[i].marks);
}

This code block takes input from the user for each structure in the array. It uses scanf with address-of operators and array indexing to populate the structure data at runtime.


🧠 Real-World Use Case: Employee Payroll System

struct Employee {
    int emp_id;
    char emp_name[40];
    float salary;
};

struct Employee staff[2] = {
    {1001, "Suresh", 55000.00},
    {1002, "Priya", 62000.00}
};

for (int i = 0; i < 2; i++) {
    printf("Employee: %s | ID: %d | Salary: %.2f\n",
           staff[i].emp_name, staff[i].emp_id, staff[i].salary);
}

This is a real-world simulation where an array of structures represents employees. The loop prints out payroll information in a readable format. This pattern is common in HR software or embedded admin panels.


🧠 Tip: Use typedef for Cleaner Syntax

typedef struct {
    int id;
    char model[20];
    float price;
} Product;

Product catalog[2] = {
    {1, "SensorX", 1299.99},
    {2, "BoardY", 1599.49}
};

Using typedef allows you to omit the struct keyword when declaring variables. This simplifies the syntax, especially for large programs.


⚠️ Common Mistakes to Avoid

MistakeWhy It’s WrongFix
class.name[1]Misunderstanding array and structUse class[1].name
Assigning strings with =Not valid in CUse strcpy()
Forgetting array sizeCan cause buffer overflowsAlways declare with known size or use malloc()

🧠 Final Thoughts

Arrays of structures allow you to store, sort, search, and manipulate large groups of related records in a clean and powerful way. Whether you’re building a student database, employee management system, or sensor data logger, mastering this concept is essential.

Key Takeaways:

  • Always use indexed access to loop through structure arrays.
  • Use typedef to improve readability.
  • Prefer designated initialization for clarity.
  • Always validate input/output while working with scanf and printf.

Still have doubts about arrays of structures in C?
Drop your code snippet or question below and get it solved!
And if this guide helped you, share it with your fellow programmers 👨‍💻👩‍💻

Leave a Comment