Array of Structure in C

Array of structure in C is used when a program needs to store multiple records of the same structure type. A single structure can hold one complete record such as one student, one employee, one sensor reading, or one book. But in real programs, we usually need many such records, not just one. That is where an array of structure becomes useful. It combines two important ideas in C: arrays and structures.

With an array of structure, each array element is a full structure object. This makes it possible to store and manage many grouped records in an organized way. It is widely used in student databases, payroll systems, product lists, inventory programs, file handling, and embedded applications where multiple related items must be processed together. In this article, we will understand array of structure in C, its syntax, declaration, initialization, member access, input and output, functions, pointers, and common mistakes.

What is Array of Structure in C?

An array of structure in C is an array where each element is a structure variable of the same type.

This means the array does not store simple values like only integers or only characters. Instead, each position in the array stores a complete record with multiple members.

An array of structure in C stores multiple structure records under one array name.

Why Array of Structure is Used in C

  • It stores multiple records of the same type in an organized form.
  • It makes looping through records easy.
  • It is useful when the same group of members repeats many times.
  • It reduces the need to create many separate structure variables manually.
  • It works well with sorting, searching, file handling, and function-based processing.

For example, if a program needs to store details of 50 students, creating 50 separate structure variables would be a poor design. An array of structure solves that problem cleanly.

Syntax of Array of Structure in C

The general syntax is:

struct StructureName
{
    data_type member1;
    data_type member2;
    data_type member3;
};

struct StructureName array_name[size];
PartMeaning
struct StructureNameStructure type definition
array_nameName of the array storing structure variables
sizeNumber of structure records in the array

Declaration of Array of Structure in C

Let us declare an array of structure to store information about students.

#include <stdio.h>

struct Student
{
    int roll;
    float marks;
    char grade;
};

int main(void)
{
    struct Student students[3];
    return 0;
}

Here students is an array containing 3 elements, and each element is a complete struct Student variable.

Initialization of Array of Structure in C

An array of structure can be initialized when it is declared.

#include <stdio.h>

struct Student
{
    int roll;
    float marks;
    char grade;
};

int main(void)
{
    struct Student students[3] =
    {
        {101, 87.5f, 'A'},
        {102, 76.0f, 'B'},
        {103, 91.0f, 'A'}
    };

    return 0;
}

Each pair of braces initializes one structure element of the array.

You can also assign values one member at a time using array indexes.

students[0].roll = 101;
students[0].marks = 87.5f;
students[0].grade = 'A';

Accessing Members of Array of Structure in C

To access a member of a structure inside the array, use the array index first and then the dot operator.

The general form is:

array_name[index].member_name

Example:

#include <stdio.h>

struct Student
{
    int roll;
    float marks;
    char grade;
};

int main(void)
{
    struct Student students[2] =
    {
        {101, 87.5f, 'A'},
        {102, 76.0f, 'B'}
    };

    printf("Roll = %d\n", students[0].roll);
    printf("Marks = %.2f\n", students[1].marks);

    return 0;
}

The expression students[1].marks means: go to the second structure in the array, then access its marks member.

Input and Output in Array of Structure in C

A loop is commonly used to read and display records stored in an array of structure.

#include <stdio.h>

struct Student
{
    int roll;
    float marks;
};

int main(void)
{
    struct Student students[2];
    int i;

    for (i = 0; i < 2; i++)
    {
        printf("Enter roll and marks: ");
        scanf("%d %f", &students[i].roll, &students[i].marks);
    }

    for (i = 0; i < 2; i++)
    {
        printf("Student %d - Roll: %d, Marks: %.2f\n",
               i + 1,
               students[i].roll,
               students[i].marks);
    }

    return 0;
}

This pattern is very common in beginner C programs and in basic data-record exercises.

Passing Array of Structure to Function in C

An array of structure can be passed to a function. Since arrays are passed by address-like behavior, the function can process the original records directly.

#include <stdio.h>

struct Student
{
    int roll;
    float marks;
};

void display(struct Student students[], int size)
{
    int i;

    for (i = 0; i < size; i++)
    {
        printf("Roll = %d, Marks = %.2f\n", students[i].roll, students[i].marks);
    }
}

int main(void)
{
    struct Student students[2] =
    {
        {101, 87.5f},
        {102, 76.0f}
    };

    display(students, 2);
    return 0;
}

This is useful when you want to separate logic such as input, display, sorting, or searching into different functions.

Pointer and Array of Structure in C

A pointer can also be used with an array of structure. The name of the array points to the first element, so pointer arithmetic can move from one structure to another.

#include <stdio.h>

struct Student
{
    int roll;
    float marks;
};

int main(void)
{
    struct Student students[2] =
    {
        {101, 87.5f},
        {102, 76.0f}
    };
    struct Student *ptr = students;

    printf("Roll = %d\n", ptr->roll);
    printf("Marks = %.2f\n", (ptr + 1)->marks);

    return 0;
}

The expression (ptr + 1)->marks accesses the marks member of the second structure in the array.

Difference Between Structure and Array of Structure in C

PointSingle StructureArray of Structure
Number of recordsOne recordMultiple records
Access styles.memberarr[i].member
Use caseOne student or one productMany students or products
Loop usageUsually not needed for accessVery useful for processing all records

A normal structure is used for one logical entity, while an array of structure is used when that entity repeats multiple times.

Memory Layout of Array of Structure in C

The structures in the array are stored one after another in memory. Each element takes the size of the full structure, including any padding added by the compiler.

This means if one structure takes 12 bytes, then the next element starts after those 12 bytes. The exact size depends on structure layout and alignment.

#include <stdio.h>

struct Student
{
    int roll;
    char grade;
    float marks;
};

int main(void)
{
    struct Student students[3];

    printf("Size of one structure = %zu\n", sizeof(struct Student));
    printf("Total size of array = %zu\n", sizeof(students));

    return 0;
}

This is important when working with file storage, communication data, and embedded memory-sensitive programs.

Common Mistakes in Array of Structure in C

  • forgetting to use the array index before accessing a member
  • confusing students.roll with students[i].roll
  • using wrong loop bounds and accessing outside the array
  • forgetting the address operator in scanf for numeric members
  • assuming all structures have no padding in memory
MistakeProblemBetter Practice
Using students.rollInvalid accessUse students[i].roll
Wrong loop limitOut-of-bounds accessLoop only up to the declared size
Ignoring paddingWrong memory assumptionsCheck structure size with sizeof

Best Practices for Array of Structure in C

  • Use meaningful structure names and member names.
  • Use loops for reading, printing, and processing records.
  • Pass the array and its size clearly to functions.
  • Be careful with input handling when the structure contains strings or mixed data types.
  • Check memory layout with sizeof when structure size matters.

FAQs

What is array of structure in C?

Array of structure in C is an array in which each element is a structure variable of the same type.

Why do we use array of structure in C?

It is used to store multiple records such as students, employees, or products in an organized way.

How do you access an array of structure in C?

Use the array index followed by the dot operator, such as students[0].roll.

Can array of structure be passed to a function in C?

Yes. It can be passed to a function along with its size, and the function can process the records.

Can we use pointers with array of structure in C?

Yes. A pointer can point to the first element of the array, and pointer arithmetic can be used to access other structure elements.

What is the difference between structure and array of structure in C?

A structure stores one record, while an array of structure stores multiple records of the same structure type.