top of page

What is an Array of Structure?

  • Writer: Siddharth Sharma
    Siddharth Sharma
  • Dec 12, 2024
  • 3 min read

Understanding Array of Structure in C Programming

In C programming, when you need to store and manage multiple records of related information, an Array of Structures is the perfect solution. This concept allows you to create a collection of structures, enabling efficient handling of data. Let’s dive into this topic and break it down into simple terms for easy understanding.


What is an Array of Structure?

An Array of Structure is a collection of structure variables stored in contiguous memory locations. Each element in the array is a structure, and all elements share the same structure type. This is especially useful when you need to store data for multiple entities, like a list of students, employees, or products.


Why Use an Array of Structure?

  • Organized Data: Keeps related data for multiple entities together.

  • Scalability: Makes it easy to handle large datasets.

  • Efficient Access: Enables direct access to any record using an index.


How to Define and Use an Array of Structure

Example: Storing Student Records

Let’s create a program to store and display the details of multiple students, such as roll number, name, and marks.


Array of Structure Program in C Language
Array of Structure Program in C Language

Output of Array of Structure in C Program
Output of Array of Structure in C Program
#include <stdio.h>

// Define the Student structure
struct Student {
    int rollNo;
    char *name;
    float marks;
};

int main() {
    // Declare an array of structures
    struct Student students[3];

    // Assign values to each element in the array
    students[0].rollNo = 101;
    students[0].name = "LAXMI";
    students[0].marks = 85.5;

    students[1].rollNo = 102;
    students[1].name = "PRIYANKA";
    students[1].marks = 90.0;

    students[2].rollNo = 103;
    students[2].name = "PANMESHWARI";
    students[2].marks = 78.5;

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

    return 0;
}

Output

Student 1:
Roll No: 101
Name: LAXMI
Marks: 85.50

Student 2:
Roll No: 102
Name: PRIYANKA
Marks: 90.00

Student 3:
Roll No: 103
Name: PANMESHWARI
Marks: 78.50


How Does It Work?

  1. Structure Definition:

    • First, define a structure to represent a single entity, such as a student.

  2. Array Declaration:

    • Declare an array of the structure type. For example, struct Student students[3]; creates an array to hold three students.

  3. Accessing Elements:

    • Use the index to access individual elements and the dot operator (.) to access structure members. For instance, students[0].name accesses the name of the first student.

  4. Loop for Input/Output:

    • Use loops to process all elements in the array efficiently.


Advantages of Array of Structure

  • Data Organization: Stores data for multiple entities in a structured way.

  • Efficient Management: Easily iterate through records using loops.

  • Logical Grouping: Keeps related information together for clarity.


Applications of Array of Structure

  1. Student Records: Managing data for multiple students.

  2. Employee Details: Storing information about employees in an organization.

  3. Product Inventory: Handling product details like ID, name, and price.

  4. Sports Scores: Recording scores of players or teams.


Key Points to Remember

  • All elements in the array must have the same structure type.

  • Use the dot operator to access members of each structure.

  • Plan the structure definition carefully to meet your data requirements.


Conclusion

The Array of Structure is a powerful tool in C programming for managing data efficiently. It simplifies working with multiple records by organizing them in a logical and structured way. Whether you are handling student records, employee details, or product inventories, this concept will help you write clean and maintainable code.

Start practicing with simple examples and gradually move to more complex use cases. Happy Coding! 😊


 
 
 

Comments


bottom of page