Nearby lessons

103 of 124

C - Arrays of Structures

Arrays of Structures is one of the foundational topics in C programming. This lesson explains Program 4: Array of Structures (Many Students) with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

Program 4: Array of Structures (Many Students)

Just like an array of ints, you can have an array of structures — for many students:

Notice the pattern s[i].rollNo — first the index, then the member.

In simple words: an array of structures is a class register — the index picks the student, the member picks the detail. s[2].marks means the marks of student number 2.
Example01
CCode Cell
1#include <stdio.h>
2 
3struct student {
4 int rollNo;
5 char name[30];
6 float marks;
7};
8 
9void main()
10{
11 struct student s[3]; // 3 students
12 int i;
13 
14 for (i = 0; i < 3; i++) {
15 printf("Enter roll, name, marks: ");
16 scanf("%d %s %f", &s[i].rollNo, s[i].name, &s[i].marks);
17 }
18 
19 printf("\nStudent details:\n");
20 for (i = 0; i < 3; i++) {
21 printf("%d %s %.2f\n", s[i].rollNo, s[i].name, s[i].marks);
22 }
23}
Output
Enter roll, name, marks: 101 Rahul 88 Enter roll, name, marks: 102 Priya 95 Enter roll, name, marks: 103 Anil 76 Student details: 101 Rahul 88.00 102 Priya 95.00 103 Anil 76.00
📝 Key Takeaways
  • Structure groups different types under one name — like a form.
  • Define the template, declare variables, access members with the dot.
  • Separate programs: access, one-line init, user input, arrays of structures.
  • Structures can nest inside structures.
  • Union shares one memory for all members; structure gives each member its own.
  • sizeof(union) = largest member; sizeof(struct) = sum of members.

🧠 Test Your Knowledge

1 Questions
Progress: 0 / 1