C Programming • Structure and Union
C Programming / Array of structures

Array of structures

Notes 4 Structure and Union

An array of structures is an array whose elements are structure variables of the same structure type. It is useful for storing multiple records of the same kind.

Notes

An array of structures is an array whose elements are structure variables of the same structure type. It is useful for storing multiple records of the same kind.

💡 Example
struct Student students[50];

Here, students can store records of 50 students.

Why Use an Array of Structures?

A single structure variable stores one record, while an array of structures can store many similar records together. This is useful for student, employee, book, and product records.

💡 Example

Instead of creating s1, s2, and s3 separately, an array can store all three student records as students[0], students[1], and students[2].

Declaration

An array of structures is declared by placing the array name and size after the structure type.

💡 Example
struct Student students[3];

This creates an array that can store three struct Student records.

Accessing Elements

An individual structure in the array is accessed using its index, and its member is then accessed using the dot operator.

💡 Example
students[0].rollNo students[1].percentage

The index selects the required student record and the dot operator accesses its member.

Using a Loop

A loop can be used to read or display all records in an array of structures. This avoids writing the same statements repeatedly.

💡 Example
for (int i = 0; i < 3; i++) printf("%d", students[i].rollNo);

The loop accesses the roll number of each student one by one.

Real-World Example

A college system may need to store information for many students. An array of structures can keep all student records under one common structure type.

🌍 Example

students[0] → Student 1
students[1] → Student 2
students[2] → Student 3

Practical Example

Problem Statement

Write a C program to store the roll number and percentage of three students using an array of structures and display all the records.

Learning Outcomes

  • Declare an array of structures.
  • Store multiple records using one structure type.
  • Use a loop to access and display structure records.

Hint

Create a Student structure and declare an array of three students. Use a for loop to read and display the values.

Theory

An array of structures combines the features of arrays and structures. Each array element is a complete structure record, and individual members are accessed using the index and dot operator.

Program

#include <stdio.h> struct Student { int rollNo; float percentage; }; int main() { struct Student students[3]; int i; // read details of three students for (i = 0; i < 3; i++) { printf("Enter roll number for student %d: ", i + 1); scanf("%d", &students[i].rollNo); printf("Enter percentage: "); scanf("%f", &students[i].percentage); } printf(" Student Records: "); // display all student records for (i = 0; i < 3; i++) { printf("Roll Number = %d, Percentage = %.1f ", students[i].rollNo, students[i].percentage); } return 0; }

Expected Output

Enter roll number for student 1: 101

Enter percentage: 82.5

Enter roll number for student 2: 102

Enter percentage: 76.0

Enter roll number for student 3: 103

Enter percentage: 88.5

Student Records:

Roll Number = 101, Percentage = 82.5

Roll Number = 102, Percentage = 76.0

Roll Number = 103, Percentage = 88.5

Note

Each element of the array is a complete structure record. For example, students[1].rollNo accesses the roll number of the second student.

Quick Revision

Concept Remember
Array of Structures Stores multiple structure records of the same type.
Declaration struct Student students[3];
Array Element Each element is one complete structure record.
Member Access students[i].rollNo
Loop Useful for processing multiple records.

Important Exam Questions

Short Answer Questions

  1. What is an array of structures?
  2. Why is an array of structures used?
  3. Write the syntax for declaring an array of structures.
  4. How do you access a structure member in an array?
  5. Why are loops useful with arrays of structures?

Long Answer Questions

  1. Explain an array of structures with declaration and suitable example.
  2. Write a C program to store and display multiple student records using an array of structures.
  3. Explain how structure members are accessed in an array of structures.
🎥 Recommended Learning

Watch a beginner-friendly explanation of arrays of structures in C programming.

▶ Watch: Array of Structures in C — Hindi

📝 Handwritten Notes

A short handwritten-style revision sheet for arrays of structures will be provided here.

🧠 Mind Map

Use the mind map for quick revision of structure array, indexing, member access and loops.