Open In App

Array of Structures in C

Last Updated : 07 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

When dealing with a large set of related data and different data types, organizing and managing it efficiently is crucial. In C programming, the combination of arrays and structures i.e. array of structures provides a powerful tool for managing that. In this article, we discuss the concept of an Array of Structures in C.

What is Array?

The array is a homogeneous collection of elements stored in the continuous memory location. The size of the array is fixed and we can randomly access the elements using their index.

Declaration of Array

array_type array_name[size];

What is Structure?

The structure is one of the user-defined data types in C that can contain elements of different types as its members.

Declaration of a Structure in C

struct structure_name{
memberType memberName;
...
...
};

Array of Structures

An array whose elements are of type structure is called array of structure. It is generally useful when we need multiple structure variables in our program.

Need for Array of Structures

Suppose we have 50 employees and we need to store the data of 50 employees. So for that, we need to define 50 variables of struct Employee type and store the data within that. However, declaring and handling the 50 variables is not an easy task. Let’s imagine a bigger scenario, like 1000 employees.

So, if we declare the variable this way, it’s not possible to handle this.

struct Employee emp1, emp2, emp3, .. . ... . ..  ... emp1000;

For that, we can define an array whose data type will be struct Employee soo that will be easily manageable.

Declaration of Array of Structures

struct structure_name array_name [number_of_elements];

Initialization of Array of Structures

We can initialize the array of structures in the following ways:

struct structure_name array_name [number_of_elements] = {
{element1_value1, element1_value2, ....},
{element2_value1, element2_value2, ....},
......
......
};

The same initialization can also be done as:

struct structure_name array_name [number_of_elements] = {
element1_value1, element1_value2 ....,
element2_value1, element2_value2 .....
};

GNU C compilers supports designated initialization for structures so we can also use this in the initialization of an array of structures.

struct structure_name array_name [number_of_elements] = {
{.element3 = value, .element1 = value, ....},
{.element2 = value, .elementN = value, ....},
......
......
};

Example of Array of Structure in C

C




// C program to demonstrate the array of structures
#include <stdio.h>
  
// structure template
struct Employee {
    char Name[20];
    int employeeID;
    int WeekAttendence[7];
};
  
// driver code
int main()
{
    // defining array of structure of type Employee
    struct Employee emp[5];
  
    // adding data
    for (int i = 0; i < 5; i++) {
        emp[i].employeeID = i;
        strcpy(emp[i].Name, "Amit");
        int week;
        for (week = 0; week < 7; week++) {
            int attendence;
            emp[i].WeekAttendence[week] = week;
        }
    }
    printf("\n");
  
    // printing data
    for (int i = 0; i < 5; i++) {
        printf("Emplyee ID: %d - Employee Name: %s\n",
               emp[i].employeeID, emp[i].Name);
        printf("Attendence\n");
        int week;
        for (week = 0; week < 7; week++) {
            printf("%d ", emp[i].WeekAttendence[week]);
        }
        printf("\n");
    }
  
    return 0;
}


Output

Emplyee ID: 0 - Employee Name: Amit
Attendence
0 1 2 3 4 5 6
Emplyee ID: 1 - Employee Name: Amit
Attendence
0 1 2 3 4 5 6
Emplyee ID: 2 - Employee Name: Amit
Attendence
0 1 2 3 4 5 6
Emplyee ID: 3 - Employee Name: Amit
Attendence
0 1 2 3 4 5 6
Emplyee ID: 4 - Employee Name: Amit
Attendence
0 1 2 3 4 5 6


Similar Reads

Array of Structures vs Array within a Structure in C
Both Array of Structures and Array within a Structure in C programming is a combination of arrays and structures but both are used to serve different purposes. Array within a Structure A structure is a data type in C that allows a group of related variables to be treated as a single unit instead of separate entities. A structure may contain element
5 min read
Array Representation in Data Structures
Representation of ArrayThe representation of an array can be defined by its declaration. A declaration means allocating memory for an array of a given size. [caption width="800"]Array[/caption]Arrays can be declared in various ways in different languages. For better illustration, below are some language-specific array declarations. C/C++ Code ; //
2 min read
Why Array Data Structures is needed?
Assume there is a class of five students and if we have to keep records of their marks in the examination then, we can do this by declaring five variables individual and keeping track of records. C/C++ Code #include &amp;lt;iostream&amp;gt; using namespace std; int main() { int v1 = 10; int v2 = 20; int v3 = 30; int v4 = 40; int v5 = 50; return 0;
3 min read
C program to store Student records as Structures and Sort them by Name
Given student's records with each record containing id, name and age of a student. Write a C program to read these records and display them in sorted order by name. Examples: Input: Student Records= { {Id = 1, Name = bd, Age = 12 }, {Id = 2, Name = ba, Age = 10 }, {Id = 3, Name = bc, Age = 8 }, {Id = 4, Name = aaz, Age = 9 }, {Id = 5, Name = az, Ag
3 min read
C program to store Student records as Structures and Sort them by Age or ID
Given student’s records with each record containing id, name and age of a student. Write a C program to read these records and display them in sorted order by age or id. Sorting by Age Examples: Input: Student Records = { {Id = 1, Name = bd, Age = 12 }, {Id = 2, Name = ba, Age = 10 }, {Id = 3, Name = bc, Age = 8 }, {Id = 4, Name = aaz, Age = 9 }, {
6 min read
C/C++ program to add N distances given in inch-feet system using Structures
Given an array arr[] containing N distances of inch-feet system, such that each element of the array represents a distance in the form of {inch, feet}. The task is to add all the N inch-feet distances using structures. Examples: Input: arr[] = { { 10, 3.7 }, { 10, 5.5 }, { 6, 8.0 } }; Output: Feet Sum: 27 Inch Sum: 5.20 Input: arr[] = { { 1, 1.7 },
4 min read
Common operations on various Data Structures
Data Structure is the way of storing data in computer's memory so that it can be used easily and efficiently. There are different data-structures used for the storage of data. It can also be defined as a mathematical or logical model of a particular organization of data items. The representation of particular data structure in the main memory of a
17 min read
What are the C programming concepts used as Data Structures
Data Types Data-type in simple terms gives us information about the type of data. Example, integer, character, etc. Data-types in C language are declarations for the variables. Data-types are classified as: Primitive or Built-in data types Some of the examples of primitive data types are as follows Variable named ch refers to the memory address 100
10 min read
C Program to Add N Distances Given in inch-feet System using Structures
Given an array arr[] containing N distances of inch-feet system, such that each element of the array represents a distance in the form of {inch, feet}. The task is to add all the N inch-feet distances using structures. Examples: Input: arr[] = { { 10, 3.7 }, { 10, 5.5 }, { 6, 8.0 } }; Output: Feet Sum: 27 Inch Sum: 5.20 Input: arr[] = { { 1, 1.7 },
3 min read
Real-life Applications of Data Structures and Algorithms (DSA)
You may have heard that DSA is primarily used in the field of computer science. Although DSA is most commonly used in the computing field, its application is not restricted to it. The concept of DSA can also be found in everyday life. Here we'll address the common concept of DSA that we use in our day-to-day lives. Application of DataStructure Appl
10 min read
Article Tags :
Practice Tags :