Array of Structures in C

Array of Structures in C with practical example program

C programming language allows you to create an array of structures. You can create an array of structures by specifying the structure type followed by the array name and size.

If you don’t know it how to create, use and initialize the array of structures in C, then don’t worry. In this blog post I will teach you almost things related to the structures array in C.

Let’s first understand what array of structure means.

Introduction to array of structure in C:

As you know an array is essentially a collection of elements of same types that store at the contiguous memory location. So, that means you can also create an array of same structure type. It helps you to create multiple instances of a same type of structure that are arranged sequentially in memory.

Array of structure make your life easy and help you to organize and manage multiple instances of the same structure type by grouping them into a single entity. This technique plays an important role in various applications where structured data handling is required, such as databases, records, simulations, and more. In the below section I will teach you to how you can declare an array of structure in C.

 

How to declare an array of structures in C?

Let’s break the declaration of structure array in the different steps for the better understanding.

 

Now it’s time to discuss the steps to declare an array of structures in C.

Step -1: Define a structure:

To create an array of structure first you have to create a structure. A structure is a user-defined data type that used to bind the different data types in a single entity and help us to manage the information in better way.

Let’s take an example:

struct Employee
{
    int id;
    char name[10];
    float salary;
};

 

Step -2: Declare the Array of Structures:

In C, an array type describes a contiguously allocated nonempty set of objects with a particular member object type, called the element type. The array types are characterized by their element type and by the number of elements in the array.

So, for array of structure type you just need to follow the below syntax,

T array_name[N];

Where, T is structure type and N is number of structure elements.

Example,

struct Employee emp[2];

 

Here emp is an array of 2 elements where each element is of type struct Employee. The emp can be used to store 2 structure variables of type struct element. You can see the below image,

c array of structures

 

 

How to access the element from the arrays of structures in C?

As we know to access individual elements of the array we use the subscript notation ([]). As an example, if you want to access the 0th element of the array then you need to write emp[0].

emp[0] : points to the 0th element of the array.

emp[1] : points to the 1st element of the array.

 

And now if you want to access the members of the structure then you need to use dot (.) operator as usual.

To access individual elements, we will use subscript notation ([]) and to access the members of each element we will use dot (.) operator as usual.

emp[0].id : refers to the id of the 0th element of the array.

emp[0].name : refers to the name of the 0th element of the array.

emp[0].salary : refers to the salary of the 0th element of the array.

Now, you are thinking how the above expression is works so if you have to revise the precedence and associative. The precedence of [] array subscript and dot(.) operator is the same but their associative is from left to right. So therefore, in the above expression first array subscript ([]) is applied followed by a dot (.)

 

Initializing array of structures:

Here I assumed that you know how to initialize the member of the structure. If you don’t know then please read this article “structure in C”.

As a normal array, you can also initialize the array of structures using the same syntax. Let’s take an example:

struct Employee
{
    int id;
    char name[NAME_SIZE];
    float salary;
};

struct Employee emp[ARRAY_SIZE] =
{
    {1,"Amlendra",1800.00},
    {2,"Pooja",100.00}
};

 

Let’s see a few C program to understand the array of structure,

Example 1:

You can initialize the array of structures during declaration using designated initializer. So, in the below code I am using the designated initializer to initialize the emp.

/**
 C Program to Initialize
 an array of structures at declaration.
*/

#include<stdio.h>

#define ARRAY_SIZE 3
#define NAME_SIZE 12

struct Employee
{
    int id;
    char name[NAME_SIZE];
    float salary;
};


int main()
{
    int index;
    /*
    Initialize an array of structures
    at declaration.
    */
    struct Employee emp[ARRAY_SIZE] =
    {
        {1,"Amlendra",1800.00},
        {2,"Puja Sharma",100.00},
        {2,"Aticleworld",100.00}
    };

    //Print array elements
    printf("\n\n");
    printf("Emp Name\tId\tSalary\n");
    for(index = 0; index < ARRAY_SIZE; ++index )
    {
        printf("%s\t%d\t%.2f\n",
               emp[index].name, emp[index].id, emp[index].salary);
    }

    return 0;
}

Output:

Emp Name        Id      Salary
Amlendra        1       1800.00
Puja Sharma     2       100.00
Aticleworld     2       100.00

 

Example 2:

You can also initialize the array of structures after its declaration by assigning the values to each structure element manually. Consider the example code,

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

#define ARRAY_SIZE 2
#define NAME_SIZE 10


struct Employee
{
    int id;
    char name[NAME_SIZE];
    float salary;
};

int main()
{
    struct Employee emp[ARRAY_SIZE];
    int index = 0;;

    for(index = 0; index < ARRAY_SIZE; ++index )
    {
        printf("\nEnter details of Employee %d\n\n", index+1);

        printf("Enter id number: ");
        scanf("%d", &emp[index].id);
        fflush(stdin);
        printf("Enter Emp name: ");

        if(fgets(emp[index].name,NAME_SIZE,stdin) == NULL)
        {
            printf("Error in reading the string\n");
            exit(1);
        }
        else
        {
            char *p = strchr(emp[index].name, '\n');
            if (p)
            {
                *p = '\0';
            }
        }
        printf("Enter Emp Salary: ");
        scanf("%f", &emp[index].salary);
    }

    printf("\n\n");

    printf("Emp Name\tId\tSalary\n");

    for(index = 0; index < ARRAY_SIZE; ++index )
    {
        printf("%s\t\t%d\t%.2f\n",
               emp[index].name, emp[index].id, emp[index].salary);
    }
    return 0;
}

Output:

Enter details of Employee 1
Enter id number: 1
Enter Emp name: Amlendra
Enter Emp Salary: 1800

Enter details of Employee 2
Enter id number: 2
Enter Emp name: Pooja
Enter Emp Salary: 100


Emp Name        Id      Salary
Amlendra        1       1800.00
Pooja           2       100.00

 

Code Explanation: In the above C program, I have created an array of structures. The size of the array is 2 which is control by the ARRAY_SIZE. You can change the array size as per your requirement. Next, I am assigning value in structure members with the help of for loop and displaying the same.

 

What is the usage of an array of structure in C:

There are a lot of places where the array of structures is used in C programming. Here I have written some articles where I have used an array of structures it would be helpful to understand.

If you want more information you can write in the comment box or you can email me.

 

How to use typedef and structure together?

 

 

Recommended posts for you: