Array of Structure in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

A structure in C is a valuable user-defined data type used for storing information. It can be coupled with an array to create an array of structures. An array of structures is defined as a collection of structure variables that can, in turn, store various entities. It is more efficient than writing multiple variables of a structure by hand.

Introduction to Array of structure in C

An array can be defined as a data structure where we can group the variables of the same data types. Each element of the array can be char, int, double, float or even a structure. We know a structure allows elements of diverse data types to be grouped under a single name. We can think of this new structure as a new data type in itself. Thus, an array can comprise elements of this new data type and is called array of that structure/ new data type.

Declaration of Array of Structure in C

Structure is a data type that gives us the ability to allow a group of interconnected variables to be thought of as one unit instead of distinct entities. A structure may contain different data types – char, int, double, float, etc. It may also include an array as its member.

The struct keyword is used to create a structure. Here is an example:

A structure variable can be declared in 2 ways:

  1. Variable and Structure declaration done together.
  1. Variable and Structure declaration done separately.

You can use this Free Online Compiler to run your C codes.

Why to Use an Array of Structures

The main advantage of an array is we can represent multiple values with a single variable. So the reusability of code improves; also, readability is increased. If there is no array of structures, we need to store many values in multiple structure variables, which is redundant.

For example, We don't need to remember structure variables like:

Instead, we can use an array of structures like:

Before, we created 3 different variables to store 3 students. Now all this can be done efficiently in one single array.

Hence, C enables us to declare an array of structures. We can evade declaring the different structure variables; instead, we can make an array containing all the structures that store the data of separate entities.

What is an Array of Structures in C

An array of structures written in C can be described as a collection of numerous structure variables containing data about different entities. It is used to hold information about various entities of diverse data types. The array of structures can also be described as a collection of structure variables.

As you can see in the below image, S is array of structure student having size 10 which means it could store information of 10 different variables of type student. So we don't need to take 10 different variables instead we could use array of structure student.

What is an Array of Structures in C

Example of Array of Structures in C

Here, we will see an example where we can take input for the marks of many students in a class using an array of structures. We would represent students as structures to store their information.

We will take the input for each information of the students with the help of a for loop and then display it accordingly.

Code:

Output:

Conclusion

  • A Structure is a data type that gives us the ability to allow a collection of interconnected variables to be treated as a single unit instead of distinct entities.
  • There are two ways of declaring the structure variable i.e with and without the structure declaration.
  • An array of structure is much more efficient than declaring multiple variables separately.
  • An array of structures written in C can be described as a collection of numerous structure variables containing data about different entities.