Array of Structures in C Programming (Complete Guide with Examples)


This tutorial explains arrays of structures in C, which allow storing multiple records of the same structure type. It covers declaration, initialization, accessing elements, and practical examples, helping beginners manage collections of complex data efficiently.

1. What is an Array of Structures

  1. An array of structures is used to store multiple structures of the same type.
  2. Useful for managing records like students, employees, etc.

Syntax:


struct structure_name array_name[size];

2. Example: Array of Student Structures


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

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

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

// Input student details
for(int i = 0; i < 3; i++) {
printf("Enter ID, Name, Marks for student %d: ", i+1);
scanf("%d", &students[i].id);
scanf(" %[^\n]", students[i].name); // input string with spaces
scanf("%f", &students[i].marks);
}

// Display student details
printf("\nStudent Records:\n");
for(int i = 0; i < 3; i++) {
printf("ID: %d, Name: %s, Marks: %.2f\n",
students[i].id, students[i].name, students[i].marks);
}

return 0;
}

Sample Output:


Enter ID, Name, Marks for student 1: 101 Muni 95.5
Enter ID, Name, Marks for student 2: 102 Pooja 88.0
Enter ID, Name, Marks for student 3: 103 Abhishek 92.0

Student Records:
ID: 101, Name: Muni, Marks: 95.50
ID: 102, Name: Pooja, Marks: 88.00
ID: 103, Name: Abhishek, Marks: 92.00

3. Key Points to Remember

  1. Array of structures stores multiple records of the same type
  2. Access members using array index and dot operator: students[i].id
  3. Useful for student databases, employee records, inventory management
  4. Can be combined with functions and pointers for dynamic operations