Learn About Array of Structure in C

Learn About Array of Structure in C

4 mins read2.6K Views Comment
Updated on Jul 7, 2023 16:51 IST

Discover how to use array structures in C to organize and store related data. Learn about creating, accessing, and manipulating arrays for efficient data management in your C programs.

2023_03_Array-of-structure-in-C.jpg

An array of structures in C is a powerful data structure that allows us to represent multiple values with a single variable. This improves the reusability of code and increases readability. By using an array of structures, we can avoid declaring multiple structure variables and instead create an array containing all the structures that store the data of separate entities. In this article, we will learn how to initialize and work with array of structures in C using examples.

Read more about C programming, if this concept is new to you.

What is an Array of Structure?

An array of structure is a data structure that stores a collection of related data items, where each item is composed of multiple data elements or fields. In an array of structure, each element of the array represents a separate instance of the structure, and the fields of the structure are accessed using dot notation.

For example, consider a program that needs to store information about a group of employees, including their names, salaries, and hire dates. Instead of creating separate variables for each employee, we can create a structure to represent an employee, with fields for name, salary, and hire date. We can then create an array of employee structures, where each element of the array represents a different employee.

What is an Array of Structure in C?

In C programming language, an array of structure is a collection of related data items of a specific user-defined data type where each element of the array represents an instance of the structure.

To define an array of structure in C, you first need to create a structure that contains the data elements you want to store. Here’s an example:

 
struct student {
char name[50];
int roll_no;
float marks;
};
Copy code

In this example, we have defined a student structure with three members: name, roll_no, and marks.

Now, to create an array of structure, you can declare an array variable of the structure type, like this:

 
struct student class[10];
Copy code

This creates an array called class that can hold up to 10 student structures. You can access the individual members of each structure using the dot operator, like this:

 
class[0].roll_no = 1;
strcpy(class[0].name, "John");
class[0].marks = 75.5;
Copy code

In this example, we are setting the values of the roll_no, name, and marks members for the first element of the class array. You can similarly set values for other elements of the array as well.

Note that the size of the array and the values of the members must be known at the time of declaration. If you need to allocate memory dynamically, you can use pointers to structures and allocate memory using malloc() or calloc() [insert blog link].

Why Use Array of Structure?

An array provides the advantage of representing multiple values with a single variable, leading to improved code reusability and readability. Storing multiple values in separate structure variables would be necessary without an array of structures, resulting in redundancy.

For instance, instead of remembering structure variables like struct student s1, s2, s3, we can use an array of structures like struct student s[3] to store data for three students in a single array efficiently.

Therefore, C allows us to declare an array of structures, avoiding declaring different structure variables and instead creating an array containing all the structures that store data for separate entities.

Check out some C programming books too!

Examples of Array of Structure in C 

Example 1: Using an array of structures as function parameters

 
#include <stdio.h>
struct point {
int x;
int y;
};
void print_points(struct point p[], int n) {
for (int i = 0; i < n; i++) {
printf("Point %d: (%d, %d)\n", i+1, p[i].x, p[i].y);
}
}
int main() {
// declare an array of 3 point structures
struct point points[3];
// set values for the points
points[0].x = 0;
points[0].y = 0;
points[1].x = 1;
points[1].y = 1;
points[2].x = 2;
points[2].y = 2;
// pass the array to a function
print_points(points, 3);
return 0;
}
Copy code

Output

2023_03_image-81.jpg

This example defines a struct point containing x and y fields. We declare an array of 3 struct point elements, and set values for each element. We then pass the array to a function print_points that takes an array of struct point elements. And an integer represents the number of elements in the array as parameters. The function prints the x and y values for each point in the array using a for loop. 

Example 2: Sorting an array of structures 

 
#include <stdio.h>
#include <string.h>
struct book {
char title[50];
char author[50];
int pages;
};
void sort_books(struct book b[], int n) {
struct book temp;
for (int i = 0; i < n-1; i++) {
for (int j = i+1; j < n; j++) {
if (strcmp(b[i].title, b[j].title) > 0) {
temp = b[i];
b[i] = b[j];
b[j] = temp;
}
}
}
}
int main() {
// declare an array of 3 book structures
struct book library[3];
// set values for the books
strcpy(library[0].title, "The Great Gatsby");
strcpy(library[0].author, "F. Scott Fitzgerald");
library[0].pages = 180;
strcpy(library[1].title, "Pride and Prejudice");
strcpy(library[1].author, "Jane Austen");
library[1].pages = 350;
strcpy(library[2].title, "To Kill a Mockingbird");
strcpy(library[2].author, "Harper Lee");
library[2].pages = 320;
// sort the books by title
sort_books(library, 3);
// print the sorted books
for (int i = 0; i < 3; i++) {
printf("Book %d\n", i+1);
printf("Title: %s\n", library[i].title);
printf("Author: %s\n", library[i].author);
printf("Pages: %d\n", library[i].pages);
printf("\n");
}
return 0;
}
Copy code

Output

2023_03_image-82.jpg

In this example, we define a struct book containing title, author, and pages fields. We declare an array of 3 struct book elements, and set values for each element. We then define a function sort_books that takes an array of struct book elements. And an integer representing the number of elements. These are in the array as parameters. The function sorts the array of books by title using a nested for loop and the strcmp function. We then print the sorted books using a for loop.

Contributed by Prerna Singh

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio