This post was first published on Jupius Learning, where you can read it free with diagrams and practice exercises.
Subscribe to our articles for regular updates.
You can put a struct inside another struct, as a field. The inner struct sits in memory as a block, with its own padding. The outer struct may have padding around that block too. The padding rules from the last post still apply, one level down.
In the first post of this series we declared a struct student, filled it and read it. This time we give that student an address, itself a struct, and we look at how the Linux kernel builds bigger structs out of smaller ones. That includes a small trick called container_of, which takes the address of an inner field and gives you back the outer struct.
All the sizes in this post come from gcc on a 64-bit x86 Linux machine. I compiled and ran every program shown here. On a different CPU or compiler a few numbers may change, and I will point out where.
How do you put a struct inside another struct?
First declare the inner type, then use it as a field of the outer type. Here is the student struct from earlier in the series, with a new addr field.
#include <stdio.h> #include <stddef.h> #include <string.h> struct address { char city[20]; int pin; }; struct student { int roll_no; char name[50]; float marks; struct address addr; }; int main(void) { struct student s1; s1.roll_no = 1; strcpy(s1.name, "Asha"); s1.marks = 88.5f; strcpy(s1.addr.city, "Pune"); s1.addr.pin = 411001; printf("sizeof(struct address) = %zu\n", sizeof(struct address)); printf("sizeof(struct student) = %zu\n", sizeof(struct student)); printf("city at offset %zu\n", offsetof(struct address, city)); printf("pin at offset %zu\n", offsetof(struct address, pin)); printf("addr at offset %zu\n", offsetof(struct student, addr)); printf("%s, pin %d\n", s1.addr.city, s1.addr.pin); return 0; } Save it as student_nested.c, then compile and run it:
gcc student_nested.c -o student_nested ./student_nested In PowerShell with MinGW, run it with .\student_nested.exe.
Output:
sizeof(struct address) = 24 sizeof(struct student) = 84 city at offset 0 pin at offset 20 addr at offset 60 Pune, pin 411001 addr is a value, not a pointer. The 24 bytes of the address live inside the student. When you copy a struct student, the city and pin go with it.
The fields of struct address add up to 20 + 4 = 24 bytes, and sizeof is also 24. There is no hole this time. city is 20 bytes, and 20 is already a multiple of 4, so pin can start right at offset 20.
The student struct in the padding post was 60 bytes. Adding a 24 byte addr at offset 60 makes it 84. The two padding bytes before marks are still there. Nesting did not remove them.
Named member or an inline definition
The usual way, which I will use for the rest of this post, is the one above. You declare struct address on its own, then write struct address addr; as a field. The member has a name, addr, and the type has a name too. You can make a standalone struct address variable, pass it to a function, or use it in more than one outer struct.
You can also write the inner struct in place, at the same time as you declare the field:
struct student { int roll_no; char name[50]; float marks; struct { char city[20]; int pin; } addr; }; You still write s1.addr.city and s1.addr.pin. The difference is that this inner type has no tag of its own, so you cannot declare a separate struct address variable. If you only need this shape inside struct student, that is fine. If you want to reuse the address type, give it a name first.
If you leave out the member name as well, the inner fields belong to the outer struct. You would then write s1.city and s1.pin, with no addr in between. gcc and C11 allow this. It can be handy, but it also hides the fact that those fields arrived as a group, so I prefer a named member for examples like this one.
How a nested struct sits in memory
The inner struct is laid out first, with the same alignment rules as any other struct. Then that whole block is placed in the outer struct. The block must start at an offset that is a multiple of the inner struct's own alignment, which is the alignment of its strictest field.
That is easier to see with a smaller example, because the holes are right next to the fields.
#include <stdio.h> #include <stddef.h> struct inner { char a; int b; }; struct outer { char flag; struct inner nest; int id; }; int main(void) { printf("sizeof(struct inner) = %zu\n", sizeof(struct inner)); printf("a at %zu, b at %zu\n", offsetof(struct inner, a), offsetof(struct inner, b)); printf("sizeof(struct outer) = %zu\n", sizeof(struct outer)); printf("flag at %zu, nest at %zu, id at %zu\n", offsetof(struct outer, flag), offsetof(struct outer, nest), offsetof(struct outer, id)); return 0; } Output:
sizeof(struct inner) = 8 a at 0, b at 4 sizeof(struct outer) = 16 flag at 0, nest at 4, id at 12 struct inner is the familiar case from the last post. a takes byte 0. b is an int, so it starts at 4, and bytes 1 to 3 are a hole. The inner struct is 8 bytes and has an alignment of 4.
In struct outer, flag takes byte 0. nest cannot start at 1, because the inner struct needs a multiple of 4. So nest starts at 4, and bytes 1 to 3 are padding in the outer struct. The 8 bytes of nest occupy 4 to 11, including the hole inside inner. id starts at 12. The outer struct is 16 bytes.
So there are two layers of padding. One hole lives inside nest, so that b is aligned. One hole lives in the outer struct, so that nest itself is aligned. If you only add up flag (1), a (1), b (4) and id (4), you get 10. sizeof says 16. The extra 6 bytes are those two holes.
How do you reach a nested field?
With a struct variable, you use a dot for the outer field and another dot for the inner one, like s1.addr.pin. With a pointer to the outer struct, you use the arrow operator to reach the inner struct, then a dot for its field, like p->addr.pin. You can also take the address of the inner struct and use the arrow operator on that.
#include <stdio.h> #include <string.h> struct address { char city[20]; int pin; }; struct student { int roll_no; char name[50]; float marks; struct address addr; }; int main(void) { struct student s1; struct student *p; struct address *ap; s1.roll_no = 1; strcpy(s1.name, "Asha"); s1.marks = 88.5f; strcpy(s1.addr.city, "Pune"); s1.addr.pin = 411001; printf("s1.addr.city = %s\n", s1.addr.city); printf("s1.addr.pin = %d\n", s1.addr.pin); p = &s1; printf("p->addr.city = %s\n", p->addr.city); printf("p->addr.pin = %d\n", p->addr.pin); ap = &s1.addr; printf("ap->city = %s\n", ap->city); printf("ap->pin = %d\n", ap->pin); return 0; } Output:
s1.addr.city = Pune s1.addr.pin = 411001 p->addr.city = Pune p->addr.pin = 411001 ap->city = Pune ap->pin = 411001 All three paths reach the same fields. ap is a pointer to the inner struct only. It does not know about roll_no or marks. The next section is about the other direction: starting from an inner pointer like ap and finding the outer student again.
The next post in this series will spend more time on pointers to structs. For this post, it is enough to know that p->addr is the same as (*p).addr, and that &s1.addr is the address of the inner block inside s1.
Copying and initialising a nested struct
You can fill a nested struct with a designated initialiser, using the field names. You can also copy the inner struct in one assignment. That copy is a copy of the bytes, not a shared object.
#include <stdio.h> #include <string.h> struct address { char city[20]; int pin; }; struct student { int roll_no; char name[50]; float marks; struct address addr; }; int main(void) { struct student s1 = { .roll_no = 1, .name = "Asha", .marks = 88.5f, .addr = { .city = "Pune", .pin = 411001, }, }; struct student s2; struct address home = { .city = "Nashik", .pin = 422001 }; memset(&s2, 0, sizeof s2); s2.roll_no = 2; strcpy(s2.name, "Rohan"); s2.marks = 76.0f; s2.addr = home; printf("s1: %s, %s, %d\n", s1.name, s1.addr.city, s1.addr.pin); printf("s2: %s, %s, %d\n", s2.name, s2.addr.city, s2.addr.pin); home.pin = 0; printf("after home.pin = 0, s2.addr.pin is still %d\n", s2.addr.pin); return 0; } Output:
s1: Asha, Pune, 411001 s2: Rohan, Nashik, 422001 after home.pin = 0, s2.addr.pin is still 422001 The .addr = { .city = "Pune", .pin = 411001 } part fills the inner struct by name. Any field you leave out is set to zero.
s2.addr = home copies all 24 bytes of home into s2. Changing home afterwards does not change s2. The two structs are separate.
You can also assign one whole student to another, s2 = s1, and the nested address is copied with it.
Read the rest on Jupius
That covers how to nest one struct in another, how the inner block is padded, and how to fill or copy a nested struct. The second half of the post moves into the Linux kernel. It shows how thread_struct sits inside task_struct, and how container_of takes an embedded list_head and gives you back the outer struct.
Read the full post on Jupius Learning. It is free, with every example, the diagrams and the practice exercises.
If you want to practice structs with small exercises that check your code as you go, the C programming course at Jupius Learning covers them step by step.
