Array of Structures in c programing with example

11/17/2023
All Articles

#Array of Structures in c programing with example

Array of Structures in c programing with example

Array of Structures in c programing with example

we aware about array behaviour 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 are aware that a structure enables the grouping of components with different data kinds under a single name.
This new structure can be considered a distinct data type in and of itself. Thus, an array can comprise elements of this new data type and is called array of that structure/ new data type.


Below is example of array of student


#include <stdio.h>
#include <string.h>
// Outer structure
struct Date {
    int day;
    int month;
    int year;
};

// Inner structure nested inside the Date structure
struct Student {
    char name[50];
    int age;
    struct Date birthdate; // Nested structure
};




int main() {
    // Declare a variable of the outer structure type
    struct Student studentArray[3];

   // Assign values to the array of members of the outer structure
    studentArray[0] = (struct Student){"developer rahul", 101, {15,01,1998}};
    studentArray[1] = (struct Student){"developer shubham", 102, {16,01,1990}};
    studentArray[2] = (struct Student){"developer push", 103,{15,04,1994}};
    


    // Access and print the values of first Array
    printf("Name of student: %s\n", studentArray[0].name);
    printf("Age: %d\n", studentArray[0].age);
    printf("Birthdate: %d/%d/%d\n", studentArray[0].birthdate.day,  studentArray[0].birthdate.month,  studentArray[0].birthdate.year);
    
        // Access and print the values of second Array
    printf("Name of student: %s\n", studentArray[1].name);
    printf("Age: %d\n", studentArray[1].age);
    printf("Birthdate: %d/%d/%d\n", studentArray[1].birthdate.day,  studentArray[1].birthdate.month,  studentArray[1].birthdate.year);
    
        // Access and print the values of third Array
    printf("Name of student: %s\n", studentArray[2].name);
    printf("Age: %d\n", studentArray[2].age);
    printf("Birthdate: %d/%d/%d\n", studentArray[2].birthdate.day,  studentArray[2].birthdate.month,  studentArray[2].birthdate.year);

    return 0;
}

Conclusion:

In this article, we are using an array allows us to represent several values with a single variable, which is its main advantage. Consequently, readability and code reusability both increase.
We must redundantly store a large number of values in several structure variables if there isn't an array of structures.
A structure in C is a valuable user-defined data type used for keeping 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  or datatype.
It is more efficient than writing multiple variables in an programming  in high level language we have option of class like java , dot net.

Article