View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

Mastering Array of Structure in C with Real Examples

Updated on 14/04/20253,660 Views

Debugging C programs often reveals a recurring need—handling related data for multiple entities. Hardcoding each variable not only clutters the code but also makes maintenance harder. That’s where the Array of Structure in C becomes a crucial construct. It simplifies data grouping, boosts clarity, and makes managing multiple records straightforward. In this article, you’ll explore everything from syntax to examples, using a simple and practical student structure throughout.

Whether you're dealing with student details, product information, or grouped inputs, structuring your data effectively is key to writing clean and efficient programs. For beginners, understanding how arrays of structures work can bridge the gap between basic and modular C code. It helps reduce redundancy, streamlines logic, and makes the debugging process far more manageable.

Pursue our Software Engineering courses to get hands-on experience!

How do you declare an array of Structures in C?

To create an array of structures in C, start by defining a structure. Use the struct keyword and group-related members under one name. Here's a simple structure for storing student details:

struct student {
    char name[50];
    char class[50];
    int roll_number;
    float marks[5];
};

This structure includes a student's name, class, roll number, and marks for five subjects. Once defined, you can create an array of this structure like so:

struct student s[3];

The array s can hold details for three different students. Each element in the array represents one student and holds its own set of member variables.

You can also declare structure variables in two ways:

Method 1: Inline Declaration

struct student {
    char name[50];
    char class[50];
    int roll_number;
    float marks[5];
} s1;

This declares a structure and a variable s1 in one step.

Must Explore: Introduction to C Tutorial

Method 2: Separate Declaration

struct student {
    char name[50];
    char class[50];
    int roll_number;
    float marks[5];
};

int main() {
    struct student s1;
}

Here, the structure is defined first. The variable s1 is declared later inside the main function.

To handle multiple records efficiently, use an array instead of repeating structure variables. This keeps the code clean and manageable.

Do check out: Array in C

Why Should You Use an Array of Structures in C?

Working with multiple entities often demands better structure and organization. An array of structures solves this problem. Here’s why it’s a smart choice:

1. Improved Reusability

With one array, you can store multiple instances of a structure. This enables you to reuse code effectively. Instead of writing repetitive logic for each variable, loops let you handle all elements with ease.

2. Cleaner and Readable Code

You don’t need to create separate variables like student1, student2, and so on. A single array, such as s[10], is enough to manage all students.

3. Efficient Memory Usage

Arrays of structures store all elements in contiguous memory locations. This improves cache performance and speeds up data access.

4. Simplified Data Management

Loops simplify input and output operations. You can use one loop to populate all student records and another to display them:

for (int i = 0; i < 3; i++) {
    // Access s[i].name, s[i].roll_number, etc.
}

5. Scalable for Future Use

If you need to handle more students, change the size of the array. The logic remains the same. You don’t have to modify your core program structure.

By using arrays of structures, your C programs become easier to scale, debug, and maintain.

Must Read: 29 C Programming Projects in 2025 for All Levels [Source Code Included]

What exactly is an array of Structures in C?

An array of structure in C is a collection of structure variables, all grouped under a single name. Each element of the array is a full structure with its own data.

Take the student example again:

struct student {
    char name[50];
    char class[50];
    int roll_number;
    float marks[5];
};

To create an array of students:

struct student s[3];

Here, s[0], s[1], and s[2] are independent student records. You can access their members like this:

s[0].name
s[1].roll_number
s[2].marks[4]

Using arrays of structures lets you manage a collection of records without cluttering the program. Instead of defining separate structure variables, you use one array and index through it. This structure also simplifies passing grouped data to functions.

To initialize this array, use loops or direct assignment. For beginners, loops are usually easier and more flexible.

Must Explore : One Dimensional Array in C

How Does an Example of Array of Structure in C Work?

Let’s walk through a complete example using the student structure. This will show how to input and display student records using an array.

#include <stdio.h>

struct student {
    char name[50];
    char class[50];
    int roll_number;
    float marks[5];
};

int main() {
    struct student s[3];

    // Input student data
    for (int i = 0; i < 3; i++) {
        printf("Enter details for student %d:\n", i + 1);

        printf("Name: ");
        scanf("%s", s[i].name);

        printf("Class: ");
        scanf("%s", s[i].class);

        printf("Roll number: ");
        scanf("%d", &s[i].roll_number);

        printf("Enter marks for 5 subjects:\n");
        for (int j = 0; j < 5; j++) {
            printf("Subject %d: ", j + 1);
            scanf("%f", &s[i].marks[j]);
        }

        printf("\n");
    }

    // Display student data
    printf("Student Records:\n");
    for (int i = 0; i < 3; i++) {
        printf("Student %d:\n", i + 1);
        printf("Name: %s\n", s[i].name);
        printf("Class: %s\n", s[i].class);
        printf("Roll number: %d\n", s[i].roll_number);

        printf("Marks: ");
        for (int j = 0; j < 5; j++) {
            printf("%.2f ", s[i].marks[j]);
        }

        printf("\n\n");
    }

    return 0;
}

Output (Sample Run)

Enter details for student 1:

Name: Rishabh

Class: 10A

Roll number: 101

Enter marks for 5 subjects:

Subject 1: 85

Subject 2: 78

Subject 3: 92

Subject 4: 88

Subject 5: 91

...

Student 1:

Name: Rishabh

Class: 10A

Roll number: 101

Marks: 85.00 78.00 92.00 88.00 91.00

This code demonstrates how arrays of structures simplify storing and processing data for multiple students in C.

Also Explore : Two Dimensional Array in C

How Does the Control Flow Work in Array of Structure in C?

Understanding how control flows through a program that uses an array of structures can make it easier to debug and maintain. The following flowchart illustrates the key stages: defining the structure, accepting input using loops, and displaying the data.

Also Explore: What are Data Structures in C & How to Use Them?

Flowchart: Input and Output Using Array of Structure

You can also checkout : Array of Pointers in C

What Can We Conclude About Array of Structure in C?

The Array of Structure in C is a must-know for anyone working with grouped data. It allows developers to:

  • Store multiple records efficiently
  • Maintain cleaner code
  • Improve memory access
  • Perform batch operations using loops

Instead of creating separate variables for each entity, you use a single array. This approach works especially well in real-world programs—whether it's handling student records, employee data, or product listings.

With arrays of structures, your C code becomes more modular, reusable, and easy to debug.

Pursue DBA in Digital Leadership from Golden Gate University, San Francisco!

FAQs About Array of Structure in C

1. What is the correct syntax to declare an array of structures in C?

struct student {
    char name[50];
    char class[50];
    int roll_number;
    float marks[5];
};

struct student s[10];  // Array of 10 student structures

2. How can I initialize an array of structures in C?

You can use a list of values during declaration:

struct student s[] = {
    {"Ram", "10A", 101, {85, 90, 88, 92, 86}},
    {"Shyam", "10B", 102, {75, 80, 78, 82, 79}},
};

3. How do I access structure members in an array?

Use the index with the dot operator:

printf("%s", s[1].name);         // Name of second student

printf("%.2f", s[0].marks[2]);   // 3rd mark of first student

4. Can I pass an array of structures to a function?

Yes. You can pass it like any other array:

void display(struct student s[], int n);

5. Is it necessary to use loops with array of structures?

Not mandatory, but highly recommended. Loops make input/output and processing more efficient and less repetitive.

6. Can we use pointers with an array of structures in C?

Yes, pointers can be used to access and manipulate elements within the array. For example:

struct student *ptr = &s[0];

7. How do we pass an array of structures to a function?

You can pass it by reference for better performance:

void display(struct student s[], int n);

8. Can we sort an array of structures in C?

Yes, use any sorting algorithm (like bubble sort) based on structure members such as roll_number or marks[0].

9. How do we search for a specific student in an array of structures?

Use a loop and compare a member value (e.g., roll_number) with the search key:

if (s[i].roll_number == target) { /* found */ }

10. Can we initialize an array of structures during declaration?

Yes, like this:
struct student s[2] = {
    {"John", "10th", 1, {88, 76, 90, 85, 92}},
    {"Amy", "10th", 2, {91, 87, 89, 93, 88}}
};

11. How do we update a value inside an array of structures?

Access the index and update directly:

s[0].marks[2] = 95.0;

12. Can we use scanf for strings in structures?

Yes, for single-word input. But use fgets() for reading full lines (e.g., names with spaces).

13. What if we access an index outside the array size?

It causes undefined behavior. Always validate array bounds before accessing.

14. Can structures contain another array of structures?

Yes. A structure can have members that are arrays of structures, depending on the program design.

15. Are arrays of structures memory efficient?

Yes, they use contiguous memory which improves performance and cache locality.

image

Take a Free C Programming Quiz

Answer quick questions and assess your C programming knowledge

right-top-arrow
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
advertise-arrow

Free Courses

Start Learning For Free

Explore Our Free Software Tutorials and Elevate your Career.

upGrad Learner Support

Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918068792934

Disclaimer

1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.

2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.