An array of structures is a collection where each element is a structure. It lets you store multiple records of the same type together, like a list of students where each student has a name and age.
Click to reveal answer
beginner
How do you declare an array of structures in C?
First, define a structure type using struct. Then declare an array of that structure type, for example: struct Student { char name[20]; int age; } students[10]; creates an array of 10 students.
Click to reveal answer
beginner
How do you access members of a structure inside an array?
Use the array index and dot operator. For example, students[0].age accesses the age of the first student. This is like picking a book from a shelf and then reading its title.
Click to reveal answer
intermediate
Can you initialize an array of structures at declaration?
Yes, you can initialize it with values like this: struct Student students[2] = { {"Alice", 20}, {"Bob", 22} }; This sets the first student's name and age, and the second student's too.
Click to reveal answer
intermediate
Why use an array of structures instead of separate arrays?
Using an array of structures keeps related data together, making code easier to read and less error-prone. For example, storing names and ages in one array of structures is clearer than two separate arrays for names and ages.
Click to reveal answer
How do you declare an array of 5 structures named 'Book' in C?
Aarray struct Book books[5];
BBook struct books[5];
Cstruct books Book[5];
Dstruct Book books[5];
✗ Incorrect
The correct syntax is struct Book books[5]; where 'Book' is the structure type and 'books' is the array name.
How do you access the 'price' member of the third element in an array 'items' of structures?
Aitems[2].price
Bitems.price[3]
Citems[3].price
Ditems->price[3]
✗ Incorrect
Array indexing starts at 0, so the third element is at index 2. Use items[2].price to access it.
Which operator is used to access a member of a structure in an array?
A* (asterisk)
B-> (arrow)
C. (dot)
D& (ampersand)
✗ Incorrect
Use the dot operator . to access members of a structure when you have the structure variable itself.
Can you initialize an array of structures at the time of declaration in C?
ANo, initialization must be done later.
BYes, using curly braces with values.
COnly for arrays of integers.
DOnly if the array size is 1.
✗ Incorrect
You can initialize an array of structures using curly braces with values for each element.
What is a benefit of using an array of structures?
AIt groups related data together for easier management.
BIt avoids using structures.
CIt makes the program run faster always.
DIt separates related data into different arrays.
✗ Incorrect
An array of structures groups related data together, making it easier to manage and understand.
Explain how to declare and use an array of structures in C with an example.
Think about a list of students with name and age.
You got /4 concepts.
Describe the advantages of using an array of structures over separate arrays for related data.
Imagine storing names and ages separately vs together.
You got /4 concepts.
Practice
(1/5)
1. What is the correct way to declare an array of 5 structures named Student in C?
easy
A. Student arr[5];
B. struct Student arr;
C. Student arr;
D. struct arr[5];
Solution
Step 1: Understand structure array declaration
To declare an array of structures, use the structure type followed by the array name and size in square brackets.
Step 2: Apply correct syntax for array of structures
Since Student is the structure type, Student arr[5]; declares an array of 5 Student structures.
Final Answer:
Student arr[5]; -> Option A
Quick Check:
Array of structures = type name[size] [OK]
Hint: Use type name[size] to declare structure arrays [OK]
Common Mistakes:
Forgetting 'struct' keyword if not typedefed
Declaring single structure instead of array
Using wrong syntax like struct arr[5];
2. Which of the following is the correct syntax to access the age member of the 3rd element in an array students of structures?
easy
A. students->age[3];
B. students.age[3];
C. students[3].age;
D. students[2].age;
Solution
Step 1: Understand zero-based indexing in arrays
Arrays in C start at index 0, so the 3rd element is at index 2.
Step 2: Access structure member using dot operator
Use students[2].age to access the age member of the 3rd element.
Final Answer:
students[2].age; -> Option D
Quick Check:
3rd element = index 2, use dot for member [OK]
Hint: Remember arrays start at 0; 3rd element is index 2 [OK]
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.