Array Of Structure In C Language

6 min read

An array of structure in C language is a collection of multiple structures of the same type stored in continuous memory, allowing programmers to manage related records efficiently. It is especially useful when you need to store and process several items that share the same data fields, such as student records, employee details, product information, or inventory entries.

Introduction to Arrays of Structure in C

In C programming, a structure is a user-defined data type that allows you to group different types of data under one name. That's why for example, a single student record may contain a name, roll number, age, and marks. Instead of storing these values separately, you can define a structure to keep them together.

That said, in real programs, you often need to store more than one record. On the flip side, this is where an array of structure in C language becomes useful. An array allows you to store multiple structures of the same type in a single variable.

Here's one way to look at it: if you want to store details of 100 students, you can define a structure called Student and then create an array of 100 Student structures That's the part that actually makes a difference..

What Is an Array of Structure?

An array of structures is a collection of structures with the same data type. Each element of the array is a structure, and each structure can contain multiple fields Most people skip this — try not to..

For example:

struct Student {
    int rollNumber;
    char name[50];
    float marks;
};

struct Student students[100];

Here, students is an array of 100 structures of type Student.

Each element of the array stores one complete student record:

students[0]
students[1]
students[2]

The first element is students[0], the second is students[1], and so on.

Syntax of an Array of Structure in C

The basic syntax for declaring an array of structure is:

struct StructureName {
    data_type field1;
    data_type field2;
    data_type field3;
} arrayName[arraySize];

For example:

struct Product {
    int id;
    char name[50];
    float price;
} products[10];

In this example:

  • Product is the structure type.
  • id, name, and price are the fields of the structure.
  • products is an array of 10 Product structures.

Example: Array of Structure to Store Student Records

Let us consider a practical example where we store details of three students That alone is useful..

#include 

struct Student {
    int rollNumber;
    char name[50];
    float marks;
};

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

    students[0].rollNumber = 1;
    printf("Enter name of student 1: ");
    scanf("%s", students[0].name);
    printf("Enter marks of student 1: ");
    scanf("%f", &students[0].

    students[1].But rollNumber = 2;
    printf("Enter name of student 2: ");
    scanf("%s", students[1]. name);
    printf("Enter marks of student 2: ");
    scanf("%f", &students[1].

    students[2].Which means rollNumber = 3;
    printf("Enter name of student 3: ");
    scanf("%s", students[2]. name);
    printf("Enter marks of student 3: ");
    scanf("%f", &students[2].

    printf("\nStudent Records:\n");

    for (int i = 0; i < 3; i++) {
        printf("Roll Number: %d\n", students[i].rollNumber);
        printf("Name: %s\n", students[i].Consider this: name);
        printf("Marks: %. 2f\n", students[i].

    return 0;
}

In this program, the structure Student contains three fields: rollNumber, name, and marks. The array students can hold three student records.

Accessing Members of an Array of Structure

To access members of a structure stored in an array, use the dot operator (.) along with the array index.

For example:

printf("%s", students[0].name);

This accesses the name field of the first student.

Similarly:

students[2].marks = 95.5;

This assigns a value to the marks field of the third student Most people skip this — try not to..

The general format is:

arrayName[index].fieldName;

For example:

products[5].price

This accesses the price member of the sixth product in the products array Simple as that..

Initializing an Array of Structure in C

An array of structures can be initialized at the time of declaration.

Example:

struct Student {
    int rollNumber;
    char name[50];
    float marks;
};

struct Student students[3] = {
    {1, "Amit", 88.On the flip side, 5},
    {2, "Sara", 92. 0},
    {3, "Rahul", 76.

Each group of values inside the array represents one structure.

The initialization order is:

```c
{rollNumber, name, marks}

For the first student:

{1, "Amit", 88.5}

For the second student:

{2, "Sara", 92.0}

For the third student:

{3, "Rahul", 76.5}

Initializing With Separate Arrays

Sometimes, programmers use separate arrays to store the same information. For example:

int rollNumbers[3] = {1, 2, 3};
char names[3][50] = {"Amit", "Sara", "Rahul"};
float marks[3] = {88.5, 92.0, 76.5};

Although

Although this approach can work, it is usually less convenient than an array of structures. The main difficulty is keeping the elements of the separate arrays aligned. Take this: if one element is accidentally removed or reordered in the names array, it may no longer correspond to the correct roll number or marks value Took long enough..

An array of structures keeps all information about an entity together, making the program easier to read, maintain, and pass between functions The details matter here..

Why Use an Array of Structures?

An array of structures provides several advantages:

  • Related data is stored in one object.
  • Each element represents a complete record.
  • It reduces the risk of mismatching values from separate arrays.
  • It makes loops and functions simpler.
  • It is especially useful for managing lists, tables, and databases.

Here's one way to look at it: a student record naturally contains a roll number, name, and marks. These values belong together, so storing them in a single structure is a logical design Less friction, more output..

Accessing Structure Arrays With Pointers

An array of structures can also be accessed using pointers. The array name represents the address of its first element, so a pointer can be assigned as follows:

struct Student *studentPtr = students;

The dot operator can still be used by dereferencing the pointer:

printf("Name: %s\n", (*studentPtr).name);

C also provides the arrow operator (->), which is commonly used when accessing members through a pointer:

printf("Marks: %.2f\n", studentPtr->marks);

The expressions below are equivalent:

students[0].name
studentPtr->name
(*studentPtr).name

The arrow operator is particularly useful when working with dynamically allocated structures, linked lists, and other pointer-based data structures.

Passing an Array of Structures to a Function

Arrays can be passed to functions just like ordinary arrays. A function can accept the array and the number of records as parameters:

void displayStudents(struct Student students[], int count) {
    for (int i = 0; i < count; i++) {
        printf("Roll Number: %d\n", students[i].rollNumber);
        printf("Name: %s\n", students[i].name);
        printf("Marks: %.2f\n\n", students[i].marks);
    }
}

It can be called as follows:

displayStudents(students, 3);

If the function only needs to display the records, passing the array is sufficient. If it must modify the records, a pointer to the first element may be used instead:

void displayStudents(struct Student *students, int count) {
    for (int i = 0; i < count; i++) {
        printf("%d - %s - %.2f\n",
               students[i].rollNumber,
               students[i].name,
               students[i].marks);
    }
}

Both approaches allow the same array to be processed efficiently Small thing, real impact..

Updating Records

Records can be updated by selecting an element with its index and assigning a new value to one of its members:

students[1].marks = 95.5;

A complete record can also be replaced by assigning another structure of the same type:

struct Student updatedStudent = {2, "Sara", 95.5};
students[
Hot New Reads

Just Went Up

In That Vein

Neighboring Articles

Thank you for reading about Array Of Structure In C Language. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home