🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →Free C Programming Course · Chapter 10 · Lecture 11
Nested Structures in C
Learn how nested structures in C let you build one structure out of another, with chained dot access, memory layout and embedded examples. Part of our free C programming course for embedded systems.
Topics in This Free C Tutorial
Real data is rarely flat. A sensor sample carries a value and the time it was taken; a network packet carries a payload and a header; a peripheral configuration carries a speed and a frame format. Nested structures in C let you model that layering directly: one structure becomes a member of another. In this lecture of our free C programming course you will declare a nested structure, reach inner members with a chain of dot operators, see exactly how the bytes are laid out, and compile and run a tested program. If you are following this free embedded C course for C programming for embedded systems, this pattern appears in almost every driver you will read.
What You Will Learn
- What nested structures in C are and why firmware uses them constantly
- How to declare an inner structure inside an outer one
- How to read and write inner members with a chained dot operator
- How the compiler lays a nested structure out in memory
- What happens when you assign one nested structure to another
- Common mistakes and how to avoid them
Prerequisites
You should already be comfortable with defining a structure, declaring structure variables and using the dot operator, which we covered in the first lectures of this chapter. Basic knowledge of printf and sizeof is enough. Any C11 compiler works; every program here was built with GCC using the flags -Wall -Wextra -std=c11.
What Are Nested Structures in C?
A structure member can have any data type: int, float, an array, a pointer, or another structure. When a member is itself a structure, the outer structure is said to nest the inner one. Nested structures in C are also called a structure within a structure. The inner structure is stored inside the outer one, byte for byte, not somewhere else in memory. That single fact explains most of the behaviour you will see in this lecture.
Idea: an outer structure that contains an inner structure
Declaring a Nested Structure
The simplest way to nest is to write the inner structure’s definition right inside the outer one and name a member of that type. Here we describe one temperature sample together with the clock time at which it was taken.
struct reading {
int sensor_id;
float temp_c;
struct stamp_in {
int hh;
int mm;
int ss;
} taken; /* member variable of the inner type */
};
Read it from the inside out. struct stamp_in is the inner template with three integers. taken is a variable of that type, and it is a member of struct reading. Notice that taken is the member name you will use in expressions, while stamp_in is only the type tag.
Accessing Nested Structure Members
To reach a member of the inner structure, apply the dot operator once per level, moving from the outside in. The dot operator associates left to right, so r1.taken.ss means: take r1, select its member taken, then select ss from that.
How r1.taken.ss is resolved
r1
The whole outer variable of type struct reading.
.taken
Select the inner structure member stored inside r1.
.ss
Select the seconds field of that inner structure.
| Expression | Meaning |
|---|---|
r1.sensor_id | Outer member, one dot |
r1.taken | The entire inner structure (a value of type struct stamp_in) |
r1.taken.hh | Hour field of the inner structure, two dots |
r1.hh | Compile error: hh is not a member of struct reading |
Complete Example: Sensor Reading With a Timestamp
Save the following as n1_nested_basic.c. The program fills one nested structure, copies it into a second one, edits the copy and prints both.
#include <stdio.h>
struct reading {
int sensor_id;
float temp_c;
struct stamp_in {
int hh;
int mm;
int ss;
} taken;
};
int main(void)
{
struct reading r1, r2;
r1.sensor_id = 7;
r1.temp_c = 36.5f;
r1.taken.hh = 9;
r1.taken.mm = 15;
r1.taken.ss = 42;
r2 = r1; /* copies the inner struct too */
r2.sensor_id = 8;
r2.taken.ss = 59;
printf("r1: id=%d temp=%.1f time=%02d:%02d:%02d\n",
r1.sensor_id, r1.temp_c, r1.taken.hh, r1.taken.mm, r1.taken.ss);
printf("r2: id=%d temp=%.1f time=%02d:%02d:%02d\n",
r2.sensor_id, r2.temp_c, r2.taken.hh, r2.taken.mm, r2.taken.ss);
struct stamp_in now = {23, 59, 59};
printf("standalone stamp: %02d:%02d:%02d\n", now.hh, now.mm, now.ss);
printf("sizeof(struct stamp_in)=%zu sizeof(struct reading)=%zu\n",
sizeof(struct stamp_in), sizeof(struct reading));
return 0;
}
Build and Run
$ gcc -Wall -Wextra -std=c11 n1_nested_basic.c -o n1
$ ./n1
r1: id=7 temp=36.5 time=09:15:42
r2: id=8 temp=36.5 time=09:15:59
standalone stamp: 23:59:59
sizeof(struct stamp_in)=12 sizeof(struct reading)=20
Three things are worth noticing in that output. First, r2 = r1 copied the inner structure along with everything else, which is why r2 printed the same hour and minute. Second, changing r2.taken.ss did not disturb r1, because the two variables own separate bytes. Third, in C the tag stamp_in is visible after the outer definition, so struct stamp_in now compiles on its own. (C++ behaves differently here, which matters if you later move code between the two languages.)
Memory Layout of a Nested Structure
Because the inner structure lives inside the outer one, the members are simply laid out one after another. With 4-byte int and float, which is typical on both desktop compilers and 32-bit microcontrollers such as ARM Cortex-M, the layout of struct reading is:
Layout of struct reading (20 bytes)
| Byte offset | Member | Belongs to | Size |
|---|---|---|---|
| 0 | sensor_id | outer | 4 |
| 4 | temp_c | outer | 4 |
| 8 | taken.hh | inner | 4 |
| 12 | taken.mm | inner | 4 |
| 16 | taken.ss | inner | 4 |
The inner structure occupies 12 bytes and the outer structure 20, exactly as the program printed. On other targets, padding may add bytes between members; always use sizeof rather than adding sizes by hand.
Why Nested Structures Matter in Embedded C Programming
In embedded C programming you will meet nested structures in three recurring roles.
| Use case | Outer structure | Inner structure |
|---|---|---|
| Time-stamped data logging | Sample with value and id | Hour, minute, second of an RTC |
| Protocol frames | Packet | Header with length and flags |
| Peripheral configuration | Device config with speed | Frame format: data bits, stop bits, parity |
Grouping related fields gives each group a name, so code reads as cfg.frame.parity instead of a flat, confusing cfg.parity next to ten unrelated fields. It also lets you pass or copy a whole group at once.
Common Mistakes With Nested Structures in C
Watch out for these:
- Skipping the inner name. Writing
r1.hhfails; the correct form isr1.taken.hh. - Using the type name instead of the member name.
r1.stamp_in.hhis wrong becausestamp_inis a tag, not a member. - Adding sizes by hand. Padding makes the real size differ on some targets. Trust
sizeof. - Leaving members uninitialised. A local nested structure holds garbage until you set every field. Start with
= {0}.
Best Practices, Performance and Safety
Good habits
- Give inner types meaningful tags and members short, clear names.
- Zero-initialise structures at declaration so no field is left undefined.
- Keep nesting shallow; three levels is usually the practical limit for readability.
- Remember that assigning a nested structure copies every byte. On a small microcontroller, passing large structures by value costs time and stack, which later lectures address with pointers.
- If a structure is sent over a wire or written to flash, check its layout with
sizeof, because padding differs between compilers.
Summary and Key Takeaways
- Nested structures in C place one structure inside another as a member.
- Use one dot per level, reading left to right, to reach inner members.
- The inner structure is stored inside the outer one, so sizes and offsets add up (plus any padding).
- Assignment of the outer structure copies the inner one as well.
- Name the member, not the type tag, when accessing inner fields.
Conclusion
Nested structures in C are a simple idea with a large payoff: they let your data types mirror the way real hardware and protocols are organised. You now know how to declare them, reach every level with chained dots, and predict their layout. In the next lecture of this free C programming course we define the inner structure outside the outer one, learn the order rule, initialise whole nested structures with braces and see which nesting is illegal.
Suggested Images for This Lecture
| Image idea | ALT text |
|---|---|
| Outer box with an inner box drawn inside it | Nested structures in C shown as an inner structure stored inside an outer structure |
| Byte-offset table for struct reading | Memory layout of nested structures in C with member offsets in bytes |
| Terminal screenshot of the build and run | Compiling and running nested structures in C with GCC in a free C programming course |
Frequently Asked Questions
What are nested structures in C?
They are structures that contain another structure as a member. The inner structure is stored inside the outer one and is reached with an extra dot operator.
How do I access a member of a nested structure?
Chain the dot operator from the outer variable inward, for example r1.taken.ss.
Does assigning one nested structure to another copy the inner part?
Yes. Structure assignment copies every byte, including inner structures, unless a member is a pointer, in which case only the pointer value is copied.
How deep can nested structures in C go?
The language sets no practical limit, but readability does. Most embedded code stays within two or three levels.
Is the inner structure stored separately in memory?
No. It occupies bytes inside the outer structure, which is why the outer size includes it.
Where is this used in embedded systems?
In logged samples with timestamps, protocol headers inside packets, and device configuration blocks with a sub-structure for frame format.
Is this free C programming course suitable for beginners?
Yes. Each lecture explains the concept first, then shows a tested program, its build command and its output.
Keep Going With the Free Embedded C Course
Continue to the next lecture to learn nested structure initialisation and the rules that keep nesting legal.

2 Comments