Array of Structures in C Programming

Get Certified in C Programming for Free and Take Your Skills to the Next Level

In C programming, an array is a group of elements given a similar name and kept in close proximity to one another in memory. An array’s elements are all the same data type. Multiple values of the same type can be stored using an array, which uses indices to access individual elements.

A structure in C is a user-defined data type that allows combining different types of data into a single type. A structure contains various members that can have different data types like int, float, char, etc. Structures help organize related data together.

We can create an array of structured data types. An array of structures in C allows for representing multiple instances of a structure as an array. Each element of the array will correspond to a single structure variable. This allows organized storage and access of data for multiple instances of a user-defined structure.

Declaration of Array of Structure in C

The ‘struct’ keyword in C is used to define a new structure data type. The general syntax is:

struct structure_name {
  data_type member1; 
  data_type member2;
  data_type member3;
  ...
};

For example, to define a structure called Student:

struct Student {
  int id;
  char name[50]; 
  float gpa;
};

To declare a structure variable, we can use:

struct structure_name variable_name;

For example:

struct Student stud1;

We can also combine structure definition and declaration together as:

struct structure_name {
  data_type member1;
  data_type member2; 
} variable_name;

For example:

struct Student {
  int id;
  char name[50];
  float gpa; 
} stud1;

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

This declares stud1 as a variable of Student structure type in one line.

Accessing Elements of an Array of Structures in C

When you have an array of structures in C, you can easily access individual elements using array indexing. Each element of the array corresponds to a single structure variable, making it straightforward to retrieve and manipulate data.

For example, if you have an array of students:

struct Student students[5];

You can access specific elements like this:

students[0] // Accesses the first student
students[1] // Accesses the second student
students[2] // Accesses the third student
// and so on...

To access specific members within a structure, you can use dot notation. For instance, to access the name of the first student, you would write:

students[0].name

This allows you to easily read and modify the attributes of each structure within the array, making it convenient to work with collections of related data.

Why to Use an Array of Structures in C

  • Using arrays allows storing multiple elements of the same type through a single variable name. This avoids declaring many separate variables.
  • An array of structures provides an efficient way of storing organized data on multiple entities of a user-defined structure type. For example, we can store data of multiple employees using an array of employee structures.
  • It improves code reusability as common operations can be done by looping through array elements rather than individual variables.
  • Accessing any element is easy through the index of the array. This is more efficient than accessing individual structure variables.
  • Compared to declaring individual structure variables, an array reduces redundancy and improves memory utilization due to contiguous allocation.
  • The code looks cleaner and more organized when using an array of structures for related data.

Imagine you are creating a program to manage information about books in a library. Each book has attributes such as title, author, ISBN, and year of publication. Storing this data in an array of structures makes the code more organized and scalable.

#include <stdio.h>
#include <string.h>

// Define a structure to represent a book
struct Book {
    char title[100];
    char author[50];
    char ISBN[13];
    int year;
};

int main() {
    // Declare an array of structures to store information about books
    struct Book library[5]; // Assuming we want to manage data for 5 books

    // Input data for each book
    for (int i = 0; i < 5; i++) {
        printf("Enter details for Book %d:\n", i + 1);
        printf("Title: ");
        scanf("%s", library[i].title);
        printf("Author: ");
        scanf("%s", library[i].author);
        printf("ISBN: ");
        scanf("%s", library[i].ISBN);
        printf("Year of Publication: ");
        scanf("%d", &library[i].year);
    }

    // Display book information
    printf("\nLibrary Catalog:\n");
    for (int i = 0; i < 5; i++) {
        printf("Book %d:\n", i + 1);
        printf("Title: %s\n", library[i].title);
        printf("Author: %s\n", library[i].author);
        printf("ISBN: %s\n", library[i].ISBN);
        printf("Year of Publication: %d\n", library[i].year);
    }

    return 0;
}

In this example, we define a structure struct Book to represent book information. We then create an array of structures library to hold data for multiple books (in this case, 5). This approach makes it easy to input and manage data for each book, and you can access and display individual attributes of each book using array indexing.

This code structure allows you to efficiently manage a library catalog with a variable number of books. You can easily add or remove books by changing the size of the array, and the code remains organized and readable. An array of structures simplifies the management of related data in a program, making it a useful tool in various real-world scenarios.

What is an Array of Structures in C

An array of structures refers to an array whose elements are of a structured data type, like a struct we define. It allows storing data on multiple instances of a structure in an organized manner through indices.

For example, declaring an array of size 10 with elements as employee structures can store data of 10 employees. Each array index stores information about one employee in the form of a structure instance.

Visually, it can be represented as:

Array of Structures

[0] -> Employee 1 structure
[1] -> Employee 2 structure
[2] -> Employee 3 structure

.
.
[9] -> Employee 10 structure

The array name points to the first structure instance by default. We can access any instance using the index, like array_name[i] would access the ith structure.

array of structures in c

Example of Array of Structures in C

Let’s take an example program to store and display data for multiple students using array of structures in C:

#include <stdio.h>

// Define Student structure
struct Student {
  int id;
  char name[50];
  float gpa;
};

int main() {

  // Declare an array of 2 Students
  struct Student record[2];
  int i;

  // Take input for 2 Students
  for(i = 0; i < 2; i++) {

    printf("Enter details for student %d\n", i + 1);

    // Input members of ith Student
    printf("Enter ID: ");
    scanf("%d", &record[i].id);

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

    printf("Enter GPA: ");
    scanf("%f", &record[i].gpa);

    printf("\n");
  }

  // Display information of 2 Students
  printf("Student Details:\n\n");

  for(i = 0; i < 2; i++) {

    printf("Student %d:\n", i + 1);
    printf("ID: %d\n", record[i].id);
    printf("Name: %s\n", record[i].name);
    printf("GPA: %.2f\n\n", record[i].gpa);
  }

  return 0;
}

Output:
Enter details for student 1
Enter ID: 101
Enter name: John
Enter GPA: 3.75

Enter details for student 2
Enter ID: 102
Enter name: Alice
Enter GPA: 3.89

Student Details:

Student 1:
ID: 101
Name: John
GPA: 3.75

ID: 102
Name: Alice
GPA: 3.89

In this example, an array ‘record’ of Student structures is declared with 5 elements. A for loop takes input from the members of each Student and stores it in the array. Another loop displays the details of each Student by accessing array elements.

This demonstrates how an array of structures can be effectively utilized to store organized data of multiple structure instances and reuse the code for input/output.

Conclusion

In C, structures allow the creation of new data types with different members. An array of structures helps store organized data on multiple instances of a user-defined structure.

The major points are:

  • Structures act as user-defined data types in C
  • An array of structures allows centralized storage of data on multiple structure instances
  • It improves code reusability, readability and efficiency
  • Accessing elements is easy using an index of the array
  • Memory utilization is improved due to contiguous allocation

The usage of arrays of structures is highly advantageous for storing data on multiple entities of a custom structure type in an efficient manner in C programs. It leads to organized code and avoids redundancy.

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

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