C Notes
Complete guide to nested structures in C — defining structures inside structures, accessing nested members, practical examples like address within employee, date within record, with output.
In real-world data modeling, entities naturally contain other entities. An employee has an address. A date has day, month, and year. An order has a customer and a product. Nested structures — placing one struct inside another — let you model these hierarchical relationships cleanly.
What is a Nested Structure?
A nested structure is simply a structure that has another structure as one of its members:
Accessing Nested Members
Use the dot operator chain: outer.inner.member
Employee: Priya Sharma (ID: 1001) Join Date: 15/03/2020 Salary: $75000.00
Memory Layout of Nested Structures
The nested struct is embedded directly — it's not a pointer to another location.
Complete Example: Student with Address
Student: Rahul Verma (Roll: 101) Home: 45 MG Road, Mumbai, Maharashtra - 400001 College: IIT Campus, Delhi, Delhi - 110016
Nested Structure Defined Inline
You can define the inner struct directly inside the outer struct:
#include <stdio.h>
struct Rectangle {
struct { // Anonymous nested struct
float x, y;
} top_left;
struct {
float x, y;
} bottom_right;
};
int main() {
struct Rectangle r = {{0.0, 10.0}, {15.0, 0.0}};
float width = r.bottom_right.x - r.top_left.x;
float height = r.top_left.y - r.bottom_right.y;
float area = width * height;
printf("Rectangle: (%.1f,%.1f) to (%.1f,%.1f)\n",
r.top_left.x, r.top_left.y,
r.bottom_right.x, r.bottom_right.y);
printf("Area: %.2f sq units\n", area);
return 0;
}Rectangle: (0.0,10.0) to (15.0,0.0) Area: 150.00 sq units
Modifying Nested Structure Members
Task: Complete C assignment Due: 25/12/2024 Status: Pending
Array of Nested Structures
Brand Model Year HP Cyl Fuel ───────────────────────────────────────────────── Toyota Camry 2023 203.0 4 Petrol Tesla Model 3 2024 283.0 0 Electric Ford F-150 2023 400.0 6 Diesel
Nesting Depth — How Deep Can You Go?
While C allows arbitrary nesting depth, deeply nested structures become hard to read. If you go beyond 2-3 levels, consider using pointers instead.
Interview Questions
Q1: What's the difference between nested structure and a pointer to another structure?
A nested structure embeds the inner struct directly (contiguous memory, value semantics — copied with assignment). A pointer to another structure stores only an address (4/8 bytes), and the actual data lives elsewhere on the heap. Use pointers for shared/optional data, and nesting for mandatory/owned data.
Q2: How do you initialize a nested structure with designated initializers?
struct Employee emp = {
.id = 101,
.name = "Alice",
.join_date = {.day = 15, .month = 6, .year = 2021},
.salary = 80000.0
};Q3: Can a structure contain a member of its own type?
Not directly (infinite size). But it can contain a *pointer* to its own type — this is how linked lists and trees work: struct Node { int data; struct Node *next; };
Q4: How is sizeof calculated for nested structures?
The sizeof the outer struct includes the full size of the inner struct (plus any padding). It's as if the inner struct's members were directly inside the outer struct, memory-wise.
Summary
- Nested structures model hierarchical "has-a" relationships
- Access nested members with chained dot operators:
outer.inner.member - Nested structs are embedded directly (not pointers) — they occupy contiguous memory
- Use pointers with
->to modify nested members via function parameters - Keep nesting depth to 2-3 levels for readability
- Nested structures are the foundation of complex data modeling in C
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Nested Structures in C – Struct Within Struct.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, structures, and, unions, nested, nested structures in c – struct within struct
Related C Programming Topics