Array of Structures in C Programming Language

Notes:

Array of Structures in C Programming Language:
Array of structures is a collection of similar type of structure variables.

Syntax for declaring array of structures is:
struct StructureName arrayName[size];
// allocates space filled with some garbage integer values

Ex:
struct Student students[3];

Syntax for declaring and initializing array of structures is:
struct StructureName arrayName[size] = { blocks of values separated by comma };
// allocates space and initializes with given initial values

Ex:
struct Student students[3] = {
{1, “Suresh”, 27},
{2, “Ramesh”, 28},
{3, “Roopesh”, 29}
};

Syntax for accessing a member in the array of structures is:
arrayName[index].membername
Ex:
students[0].rollnum; // 1

Example Code:

#include <stdio.h>

int main()
{
struct Student
{
int rollnum;
char *name;
int age;
};

struct Student students[3] = {
{1, "Suresh", 27},
{2, "Ramesh", 28},
{3, "Roopesh", 29}
};
int i=0;

for(i=0; i<3; i++)
{
printf("students[%d] rollnum=%d\n",i,students[i].rollnum);
printf("students[%d] name=%s\n",i,students[i].name);
printf("students[%d] age=%d\n",i,students[i].age);
printf("\n");
}

return 0;
}