Nested structures - Time & Space Complexity
When working with nested structures in C, it's important to see how the program's work grows as the data size grows.
We want to know how the time to process nested structures changes when the input gets bigger.
Analyze the time complexity of the following code snippet.
struct Point {
int x;
int y;
};
struct Rectangle {
struct Point corners[4];
};
void printRectangles(struct Rectangle rects[], int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < 4; j++) {
printf("(%d, %d) ", rects[i].corners[j].x, rects[i].corners[j].y);
}
printf("\n");
}
}
This code prints the coordinates of each corner of multiple rectangles stored in an array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The inner loop prints 4 corners for each rectangle.
- How many times: The outer loop runs n times, and for each, the inner loop runs 4 times.
As the number of rectangles (n) grows, the total prints grow proportionally because each rectangle always has 4 corners.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 40 (10 x 4) |
| 100 | 400 (100 x 4) |
| 1000 | 4000 (1000 x 4) |
Pattern observation: The total work grows directly with n, since the inner loop count is fixed.
Time Complexity: O(n)
This means the time to print all rectangles grows in a straight line as the number of rectangles increases.
[X] Wrong: "Because there are two loops, the time complexity must be quadratic (O(n²))."
[OK] Correct: The inner loop runs a fixed 4 times, not depending on n, so it does not multiply n by n.
Understanding how nested structures affect time helps you explain your code clearly and shows you can think about efficiency in real projects.
"What if the number of corners in each rectangle was not fixed at 4 but depended on input size m? How would the time complexity change?"
Practice
Solution
Step 1: Understand structure composition
A nested structure means one structure is included inside another as a member.Step 2: Rule out distractors
The other options describe unrelated concepts: a structure using pointers only, with only one member, or declared inside a function.Final Answer:
A structure that contains another structure as a member -> Option AQuick Check:
Nested structure = structure inside structure [OK]
- Thinking nested means pointer usage
- Confusing nested with single-member structures
- Assuming nested means declared inside functions
Solution
Step 1: Identify access operator for nested structures
When using nested structures (not pointers), use dot notation for each level: outer.inner.member.Step 2: Understand pointer vs non-pointer access
Arrow (->) is used only when accessing members through pointers, which is not stated here.Final Answer:
outer.inner.member -> Option CQuick Check:
Use dot for nested struct members [OK]
- Using arrow (->) when not using pointers
- Mixing dot and arrow incorrectly
- Confusing member access syntax
typedef struct {
int day;
int month;
int year;
} Date;
typedef struct {
char name[20];
Date birthday;
} Person;
int main() {
Person p = {"Alice", {15, 8, 1990}};
printf("%d-%d-%d", p.birthday.day, p.birthday.month, p.birthday.year);
return 0;
}Solution
Step 1: Understand structure initialization
Person p is initialized with name "Alice" and birthday as Date {15, 8, 1990}.Step 2: Trace printf output
printf prints p.birthday.day (15), p.birthday.month (8), p.birthday.year (1990) separated by dashes.Final Answer:
15-8-1990 -> Option AQuick Check:
Print nested struct members correctly [OK]
- Printing name instead of date
- Mixing order of day, month, year
- Forgetting to initialize nested struct
struct Address {
char city[20];
int zip;
};
struct Employee {
char name[30];
struct Address addr;
};
int main() {
struct Employee emp;
emp.addr->zip = 12345;
return 0;
}Solution
Step 1: Check member access syntax
emp.addr is a structure variable, not a pointer, so '->' operator is invalid here.Step 2: Correct operator usage
Use dot operator '.' to access zip: emp.addr.zip = 12345;Final Answer:
Using '->' operator on a non-pointer structure variable -> Option BQuick Check:
Use '.' for struct variables, '->' for pointers [OK]
- Using '->' on struct variables
- Confusing pointer and non-pointer access
- Ignoring compiler error messages
Person and returns the birth year?
typedef struct {
int day;
int month;
int year;
} Date;
typedef struct {
char name[50];
Date birthday;
} Person;
// Function prototype here
Which of the following is the correct function definition?Solution
Step 1: Understand parameter passing
The function takes a Person by value, so use dot notation to access members.Step 2: Access nested member correctly
Access birthday.year as p.birthday.year since p is not a pointer.Final Answer:
int getBirthYear(Person p) { return p.birthday.year; } -> Option DQuick Check:
Use dot for struct value parameters [OK]
- Using arrow (->) on non-pointer parameter
- Accessing wrong member name
- Confusing pointer and value parameter syntax
Ready to prove it?
Pass the test and this pattern moves unknown
