0
0
Cprogramming~10 mins

Nested structures - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Nested structures
Define inner struct
Define outer struct with inner struct inside
Create outer struct variable
Assign values to inner struct fields
Assign values to outer struct fields
Access and use nested fields
Program output or further use
This flow shows how an inner structure is defined and used inside an outer structure, then how values are assigned and accessed step-by-step.
Execution Sample
C
struct Point {
    int x;
    int y;
};

struct Rectangle {
    struct Point topLeft;
    struct Point bottomRight;
};
Defines two structures: Point and Rectangle, where Rectangle contains two Points as nested structures.
Execution Table
StepActionVariable/FieldValueNotes
1Define struct PointPointDefinedStructure with fields x and y
2Define struct RectangleRectangleDefinedContains two Point structs: topLeft and bottomRight
3Create variable rect of type RectanglerectUninitializedMemory allocated for rect
4Assign rect.topLeft.x = 1rect.topLeft.x1Set x of topLeft point
5Assign rect.topLeft.y = 2rect.topLeft.y2Set y of topLeft point
6Assign rect.bottomRight.x = 3rect.bottomRight.x3Set x of bottomRight point
7Assign rect.bottomRight.y = 4rect.bottomRight.y4Set y of bottomRight point
8Access rect.topLeft.xrect.topLeft.x1Read value 1
9Access rect.bottomRight.yrect.bottomRight.y4Read value 4
10End of exampleAll nested fields assigned and accessed
💡 All nested structure fields assigned and accessed successfully.
Variable Tracker
VariableStartAfter Step 4After Step 5After Step 6After Step 7Final
rect.topLeft.xundefined11111
rect.topLeft.yundefinedundefined2222
rect.bottomRight.xundefinedundefinedundefined333
rect.bottomRight.yundefinedundefinedundefinedundefined44
Key Moments - 2 Insights
Why do we write rect.topLeft.x instead of just rect.x?
Because x is a field inside the nested struct Point, which is inside rect as topLeft. The execution_table rows 4 and 8 show accessing nested fields requires specifying each level.
Can we assign values to rect.topLeft and rect.bottomRight directly?
Yes, but only by assigning a whole Point struct. Here, we assign individual fields step-by-step as shown in rows 4 to 7 for clarity.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 6, what is the value of rect.bottomRight.x?
A3
B4
C2
Dundefined
💡 Hint
Check the 'Value' column at step 6 in the execution_table.
At which step is rect.topLeft.y assigned a value?
AStep 4
BStep 5
CStep 6
DStep 7
💡 Hint
Look for assignment actions to rect.topLeft.y in the execution_table.
If we tried to access rect.x directly, what would happen?
AIt would give the value of rect.topLeft.x
BIt would give the value of rect.bottomRight.x
CIt would cause a compile error
DIt would return zero
💡 Hint
Recall that x is inside nested structs, not directly inside rect; see key_moments explanation.
Concept Snapshot
Nested structures in C allow one struct to contain another.
Use dot notation to access inner fields: outer.inner.field.
Assign values step-by-step to nested fields.
Access nested fields by specifying each level.
Useful for grouping related data hierarchically.
Full Transcript
This example shows how nested structures work in C. First, we define an inner struct Point with fields x and y. Then, we define an outer struct Rectangle that contains two Points named topLeft and bottomRight. We create a variable rect of type Rectangle. We assign values to each nested field step-by-step: rect.topLeft.x, rect.topLeft.y, rect.bottomRight.x, and rect.bottomRight.y. Finally, we access these nested fields to use their values. This demonstrates how to organize data inside nested structures and how to access and assign their fields correctly.