Table of contents
1.
Introduction
2.
What is Array?
3.
What is Structure?
4.
Array of Structures
5.
Why Use an Array of Structures?
6.
Need for Array of Structures
7.
Implementation in C
8.
Example of Array of Structure in C
9.
Frequently Asked Questions
9.1.
What are structures?
9.2.
In C, what is the difference between the terms struct and union?
9.3.
What is dynamic data structure?
10.
Conclusion
Last Updated: Dec 10, 2024
Easy

Array of Structures in C

Author Saksham Gupta
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In C programming, structures provide a way to group related data types under a single name, making it easier to manage complex data. But what if you need to work with multiple instances of such structured data? This is where Array of Structures becomes invaluable.

An array of structures allows you to store and manipulate multiple records, each having the same data layout, using a single entity. For example, you can use it to manage a list of employees, students, or products, where each element in the array represents an individual record. In this blog, we will guide you through the concept of arrays of structures in C.

Introductory Image

Also See, Sum of Digits in C

What is Array?

An array in C is a collection of elements of the same data type, stored in contiguous memory locations. It allows efficient access and manipulation of multiple values using a single identifier and an index. Arrays are ideal for managing lists, such as numbers or strings, in a structured way.

What is Structure?

Structure in C language is a user-defined data type that enables us to store the collection of different data types. Let's say I have to create a structure for a student which will store different properties such as age, roll-no. Then we will have the following code.

struct student{
   int age;
   int roll_no;
};


Now, what if there are 3 students in class and we have to store their data. It would be very difficult to create 3 different structures for each student and then use them. Here our arrays come to the rescue. We can create an array of structures that will store the data of all these 3 students in one place; thus, it can be easily accessed. Let’s see how we can do this by the following code.

You can also read about the dynamic arrays in c

Array of Structures

An Array of Structures in C is a combination of two powerful features: arrays and structures. It allows you to store multiple instances of a structure in a single, indexed collection. Each element in the array is a structure, making it an efficient way to handle complex, related data sets.

For instance, if you have a structure to represent a student with attributes like name, roll number, and marks, an array of structures can store details of multiple students. You can access and modify individual elements using their index, just like with a regular array.

Example

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

struct Student students[5]; // Array of 5 structures  

 

This approach simplifies data management in scenarios involving a collection of records, such as a list of employees, books, or transactions.

Why Use an Array of Structures?

Using an array of structures offers several benefits:

  • Efficient Data Organization: Combines related attributes into a single structure, making it easy to group and handle multiple records.
  • Ease of Access: Allows indexed access to individual records, simplifying data retrieval and manipulation.
  • Scalability: Facilitates handling large datasets, such as thousands of records, using a single, manageable array.
  • Flexibility: Enables the creation of homogeneous collections of records, streamlining operations like sorting or searching.
  • Code Readability: Reduces clutter by organizing data logically, improving program structure and maintainability.

Need for Array of Structures

In real-world applications, programs often need to manage a collection of related yet distinct records, like employee details, student grades, or product catalogs. An array of structures meets this need by providing:

  • Uniform Representation: Ensures that all records share the same attributes and structure.
  • Simplified Data Management: Helps handle large datasets by consolidating individual records into a single, organized entity.
  • Logical Grouping: Groups attributes related to each record, reducing redundancy and errors compared to managing separate arrays for each attribute.
  • Practical Usage: Ideal for tasks like creating databases, implementing algorithms that process structured data, or building user-defined data types.

Implementation in C

#include <stdio.h>
 
struct student
{
   int age;
   int roll_no;
   int marks;
};
 
int main(void)
{
 
   // Declaring array of structure.
   struct student arr[3];
 
   // Details of the first student.
   arr[0].marks = 80;
   arr[0].age = 17;
   arr[0].roll_no = 21;
 
   // Details of the second student.
   arr[1].marks = 90;
   arr[1].age = 18;
   arr[1].roll_no = 22;
 
   // Details of the third student.
   arr[2].marks = 80;
   arr[2].age = 19;
   arr[2].roll_no = 23;
 
   // Printing Details of the second student.
   printf("%s \n", "Details of the second student are ");
   printf("%s %d \n", "Age", arr[1].age);
   printf("%s %d \n", "Marks", arr[1].marks);
   printf("%s %d \n", "Roll Number", arr[1].roll_no);
 
   return 0;
}
You can also try this code with Online C Compiler
Run Code


Output

Details of the second student are  
Age 18 
Marks 90 
Roll Number 22 

So, we can see how easily we can access the student’s data as well.

Example of Array of Structure in C

Below is an example demonstrating how to use an array of structures in C to store and display information about multiple students.

Example:

#include <stdio.h>  

// Define a structure for Student  
struct Student {  
    char name[50];  
    int roll;  
    float marks;  
};  

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

    // Input details for each student  
    for (int i = 0; i < 3; i++) {  
        printf("Enter details for student %d:\n", i + 1);  
        printf("Name: ");  
        scanf("%s", students[i].name);  
        printf("Roll Number: ");  
        scanf("%d", &students[i].roll);  
        printf("Marks: ");  
        scanf("%f", &students[i].marks);  
    }  

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

    return 0;  
}  
You can also try this code with Online C Compiler
Run Code

 

Output:

Enter details for student 1:  
Name: Rahul
Roll Number: 101  
Marks: 95.5  

Enter details for student 2:  
Name: Rohit
Roll Number: 102  
Marks: 88.0  

Enter details for student 3:  
Name: Mohit
Roll Number: 103  
Marks: 92.3  

Student Details:  
Student 1: Name = Rahul, Roll Number = 101, Marks = 95.50  
Student 2: Name = Rohit, Roll Number = 102, Marks = 88.00  
Student 3: Name = Mohit, Roll Number = 103, Marks = 92.30  

 

Explanation:

  1. Structure Definition: The struct Student defines attributes like name, roll, and marks for each student.
  2. Array Declaration: The array students[3] is declared to store the details of three students.
  3. Input Loop: A for loop is used to collect data for each student by accessing the array elements using their index.
  4. Output Loop: Another for loop prints the stored details, showcasing how each element in the array is a complete structure.

Frequently Asked Questions

What are structures?

Structure in C language is a user-defined data type that enables us to store the collection of different data types. Let's say I have to create a structure for a student which will store different properties such as age, and roll-no.

In C, what is the difference between the terms struct and union?

A struct is a collection of complex data structures stored in a memory block, each with its own memory location to make them all accessible at the same time. In contrast, in the union, all of the member variables are stored in the same memory location, which means that changing the value of one member variable will change the value of all other members.

What is dynamic data structure?

A dynamic data structure (DDS) is a memory organization or collection of data that can expand or contract in size, allowing a programmer to precisely control how much memory is used. Unused memory is allocated or de-allocated from the heap as needed to change the size of dynamic data structures.

Conclusion

In this article, We have discussed the structure that is available in many programming languages, and how we can implement an array of structures. Lastly, we implemented the program in C programming language.

If you think this blog has helped you enhance your knowledge about the above question, and if you would like to learn more, check out our articles 

Live masterclass