Nested structures let you put one structure inside another. This helps organize related data clearly, like putting a smaller box inside a bigger box.
Nested structures in C++
Introduction
Syntax
C++
struct Outer {
int id;
struct Inner {
int value;
} inner;
};You define a structure inside another by writing one struct inside another.
The inner structure becomes a member of the outer structure and can be accessed using the dot operator.
Examples
Person structure with a nested Address structure inside it.C++
struct Person {
struct Address {
int houseNumber;
char street[50];
} address;
char name[50];
};Date is a separate structure used inside Event to store the event's date.C++
struct Date {
int day;
int month;
int year;
};
struct Event {
char title[100];
Date eventDate;
};C++
struct Outer {
int id;
struct Inner {
int value;
} inner;
};
Outer o;
o.id = 1;
o.inner.value = 10;Sample Program
This program creates a Person with a nested Address. It sets the name and address, then prints them.
C++
#include <iostream>
#include <cstring>
struct Person {
struct Address {
int houseNumber;
char street[50];
} address;
char name[50];
};
int main() {
Person p;
std::strcpy(p.name, "Alice");
p.address.houseNumber = 123;
std::strcpy(p.address.street, "Maple Street");
std::cout << "Name: " << p.name << "\n";
std::cout << "House Number: " << p.address.houseNumber << "\n";
std::cout << "Street: " << p.address.street << "\n";
return 0;
}Important Notes
You access nested structure members by chaining the dot operator, like outer.inner.member.
Nested structures help keep related data together and make your code easier to understand.
Summary
Nested structures let you put one structure inside another to organize data.
Use the dot operator to access inner structure members.
This helps keep your code clean and data grouped logically.
Practice
1. What is the main purpose of using
nested structures in C++?easy
Solution
Step 1: Understand nested structures concept
Nested structures let you put one structure inside another to group related data logically.Step 2: Compare options
Only To group related data logically inside another structure correctly describes grouping data logically. Other options describe unrelated concepts.Final Answer:
To group related data logically inside another structure -> Option DQuick Check:
Nested structures = group data logically [OK]
Hint: Think of nested structures as folders inside folders [OK]
Common Mistakes:
- Confusing nested structures with inheritance
- Thinking nested structures increase structure size unnecessarily
- Assuming nested structures add functions
2. Which of the following is the correct syntax to access member
city inside a nested structure address within person?easy
Solution
Step 1: Identify access operator for nested structures
When accessing members of nested structures by variable (not pointer), use dot operator for each level.Step 2: Apply to given example
Sincepersonis a variable, accessaddresswith dot, thencitywith dot:person.address.city.Final Answer:
person.address.city -> Option BQuick Check:
Use dot for nested struct members when variable, not pointer [OK]
Hint: Use dot operator for nested struct members if not pointers [OK]
Common Mistakes:
- Using arrow operator (->) when variable is not a pointer
- Mixing dot and arrow operators incorrectly
- Trying to access nested member directly without parent
3. What will be the output of the following code?
struct Date {
int day;
int month;
};
struct Event {
char name[10];
Date date;
};
int main() {
Event e = {"Party", {25, 12}};
std::cout << e.name << " on " << e.date.day << "/" << e.date.month << std::endl;
return 0;
}medium
Solution
Step 1: Understand structure initialization
Event e is initialized with name "Party" and Date with day=25, month=12.Step 2: Trace output statement
Output prints e.name, then e.date.day (25), then e.date.month (12) separated by slashes.Final Answer:
Party on 25/12 -> Option AQuick Check:
Nested struct initialized correctly = Party on 25/12 [OK]
Hint: Match initialization order with nested structure fields [OK]
Common Mistakes:
- Swapping day and month values
- Assuming compilation error due to nested struct
- Forgetting to use dot operator to access nested members
4. Identify the error in the following code snippet:
struct Point {
int x, y;
};
struct Rectangle {
Point topLeft;
Point bottomRight;
};
int main() {
Rectangle rect;
rect.topLeft->x = 0;
rect.topLeft->y = 10;
return 0;
}medium
Solution
Step 1: Check member access operators
rect.topLeft is a structure variable, not a pointer, so dot operator (.) must be used, not arrow (->).Step 2: Identify error cause
Using -> on non-pointer causes compilation error.Final Answer:
Using arrow operator (->) on a non-pointer structure variable -> Option AQuick Check:
Use dot for struct variables, arrow for pointers [OK]
Hint: Use dot for struct variables, arrow only for pointers [OK]
Common Mistakes:
- Confusing dot and arrow operators
- Assuming arrow works for all struct members
- Ignoring compiler error messages
5. Given the following nested structures, how would you write a function
printBookInfo that prints the book title and the author's city?struct Author {
char name[50];
char city[30];
};
struct Book {
char title[100];
Author author;
};hard
Solution
Step 1: Understand structure member access
Book variable b has member title and nested structure author. Access author.city with dot operator.Step 2: Check each option
void printBookInfo(Book b) { std::cout << b.title << ", " << b.author.city << std::endl; } correctly uses b.title and b.author.city with dot operators. Others misuse arrow or omit author.Final Answer:
void printBookInfo(Book b) { std::cout << b.title << ", " << b.author.city << std::endl; } -> Option CQuick Check:
Access nested struct members with dot: b.author.city [OK]
Hint: Use dot operator for nested struct members in function parameters [OK]
Common Mistakes:
- Using arrow operator on non-pointer parameter
- Skipping nested struct name when accessing member
- Using pointer syntax incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
