An array of structures lets you store many related groups of data together. It helps organize information like a list of people, each with their own details.
When you want to keep a list of students, each with a name, age, and grade.
When you need to store multiple books, each with a title, author, and year.
When you want to manage a collection of products, each with an ID, price, and quantity.
When you want to group related data items together and access them by index.
Syntax
C
struct Person {
char name[50];
int age;
float height;
};
struct Person people[10];
You first define the structure type with struct.
Then you create an array of that structure type to hold many items.
Examples
This creates an array of 5 books, each with title, author, and year.
C
struct Book {
char title[100];
char author[50];
int year;
};
struct Book library[5];
An empty array of structures (size 0) means no elements are stored yet.
C
struct Point {
int x;
int y;
};
struct Point points[0];
An array with one element can store exactly one item.
The array items has two elements: first with id=1, name="Pen"; second with id=2, name="Book".
Step 2: Understand printf access
items[1].name accesses the second element's name, which is "Book".
Final Answer:
Book -> Option B
Quick Check:
items[1].name = "Book" [OK]
Hint: Index 1 accesses second element's name [OK]
Common Mistakes:
Confusing index 0 and 1
Expecting id to print instead of name
Syntax errors in initialization
4. What is wrong with this code snippet?
struct Point {
int x, y;
};
struct Point points[3];
points[0].x = 10;
points[0].y = 20;
points[1].x = 30;
points[1].y = 40;
points[2].x = 50;
points[2].y = 60;
printf("%d", points[3].x);
medium
A. Array size should be 4 instead of 3
B. Missing semicolon after struct definition
C. Accessing points[3] is out of bounds
D. Incorrect struct member access syntax
Solution
Step 1: Check array size and indexing
The array points has size 3, valid indices are 0, 1, 2.
Step 2: Identify invalid access
Accessing points[3] is out of bounds and causes undefined behavior.
Final Answer:
Accessing points[3] is out of bounds -> Option C
Quick Check:
Array index must be less than size [OK]
Hint: Array indices go from 0 to size-1 only [OK]
Common Mistakes:
Using index equal to array size
Ignoring array bounds errors
Assuming array auto-expands
5. You have a structure Employee with members int id; and float salary;. How do you write a function in C that takes an array of Employee and its size, then returns the average salary?
hard
A. float averageSalary(Employee emp[], int n) { float sum=0; for(int i=0;i
B. float averageSalary(Employee *emp, int n) { float sum=0; for(int i=0;isalary; return sum/n; }
C. float averageSalary(Employee emp[], int n) { float sum=0; for(int i=0;i
D. float averageSalary(Employee emp[], int n) { float sum=0; for(int i=0;i
Solution
Step 1: Loop through array elements correctly
Use a for loop from 0 to n-1 and access each element with emp[i].
Step 2: Sum the salary member of each element
Access salary as emp[i].salary and add to sum, then divide by n.
Final Answer:
float averageSalary(Employee emp[], int n) { float sum=0; for(int i=0;i -> Option A
Quick Check:
Loop + emp[i].salary sum / n = average [OK]
Hint: Use emp[i].member to access array struct members [OK]
Common Mistakes:
Using emp->salary inside loop instead of emp[i].salary
Accessing emp.salary instead of emp[i].salary
Adding whole struct instead of member
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.