Structs Inside Structs in C: Nesting, Memory and container_of (Linux Kernel Examples)
Nest one C struct inside another, see how the inner struct sits in memory with padding, and how the Linux kernel uses container_of to get the outer struct back.

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.
Nested structs in the Linux kernel: thread_struct inside task_struct
The Linux kernel uses this all the time. The process descriptor, struct task_struct, holds a struct thread_struct named thread. That inner struct is the CPU-specific state of the task: the kind of thing that belongs to one architecture, like saved registers. It lives at the end of task_struct. This is from include/linux/sched.h:
/* From the Linux kernel, include/linux/sched.h */
/* CPU-specific state of this task: */
struct thread_struct thread;
/*
* WARNING: on x86, 'thread_struct' contains a variable-sized
* structure. It *MUST* be at the end of 'task_struct'.
*
* Do not put anything below here!
*/
We walked through the fields of task_struct in task_struct in the Linux Kernel: Every Field Explained. The point here is the nesting. thread is not a pointer. It is a struct thread_struct stored inside the task. The kernel can write task->thread and reach those fields the same way we wrote p->addr.pin.
The warning is about layout. On x86, thread_struct can grow past a fixed size, so it has to be the last field. If someone added another field after it, that field would sit at the wrong place. Nesting is not only a way to group data. Sometimes the position of the inner struct in the outer one matters too.
The same kernel file also embeds smaller structs that are used as hooks, not as a bundle of data. That is the list_head pattern, and it leads to container_of.
Embedding list_head, and finding the outer struct with container_of
A Linux kernel linked list does not point at the whole object it links. It points at a small struct that has been placed inside that object:
/* From the Linux kernel, include/linux/types.h */
struct list_head {
struct list_head *next, *prev;
};
On a 64-bit machine that is two pointers, 16 bytes, with an alignment of 8. struct task_struct embeds more than one of them. tasks links every process into the kernel's process list. children and sibling link a process to its family. There are others too.
/* From the Linux kernel, include/linux/sched.h */
struct list_head tasks;
/* ... */
struct list_head children;
struct list_head sibling;
The list code only sees a struct list_head. It does not know whether that hook sits inside a task_struct or some other type. When the kernel is walking the process list and has a pointer to a tasks field, it needs the task_struct that contains it. That is what container_of is for.
offsetof, which we used in the last post to find padding, is the first half of the idea. If you know that tasks starts 24 bytes into a struct, then the start of the struct is 24 bytes before the tasks pointer you have. container_of does that subtraction for you.
Here is a small program that copies the idea, without kernel headers. The struct task is a stand-in, not the real task_struct. The list_head sits in the middle, not at the start, which is the interesting case.
#include <stdio.h>
#include <stddef.h>
#include <string.h>
struct list_head {
struct list_head *next;
struct list_head *prev;
};
#define container_of(ptr, type, member) \
((type *)((char *)(ptr) - offsetof(type, member)))
struct task {
int pid;
char comm[16];
struct list_head tasks;
int prio;
};
int main(void)
{
struct task t;
struct list_head *hook;
struct task *found;
ptrdiff_t gap;
t.pid = 42;
strcpy(t.comm, "demo");
t.prio = 120;
t.tasks.next = &t.tasks;
t.tasks.prev = &t.tasks;
printf("sizeof(struct list_head) = %zu\n", sizeof(struct list_head));
printf("sizeof(struct task) = %zu\n", sizeof(struct task));
printf("pid at offset %zu\n", offsetof(struct task, pid));
printf("comm at offset %zu\n", offsetof(struct task, comm));
printf("tasks at offset %zu\n", offsetof(struct task, tasks));
printf("prio at offset %zu\n", offsetof(struct task, prio));
hook = &t.tasks;
gap = (char *)hook - (char *)&t;
found = container_of(hook, struct task, tasks);
printf("t.tasks is %td bytes from the start of t\n", gap);
printf("found->pid = %d\n", found->pid);
printf("found->comm = %s\n", found->comm);
printf("found->prio = %d\n", found->prio);
printf("found is the same object as t: %s\n", found == &t ? "yes" : "no");
return 0;
}
Output:
sizeof(struct list_head) = 16
sizeof(struct task) = 48
pid at offset 0
comm at offset 4
tasks at offset 24
prio at offset 40
t.tasks is 24 bytes from the start of t
found->pid = 42
found->comm = demo
found->prio = 120
found is the same object as t: yes
pid takes bytes 0 to 3. comm takes 4 to 19. The next free byte is 20, but tasks is a list_head, so it needs an alignment of 8. It starts at 24, and bytes 20 to 23 are padding. tasks occupies 24 to 39. prio starts at 40. The whole struct has the alignment of list_head, which is 8, so the size is rounded up from 44 to 48. That last pad is the same end-padding rule as in the last post, caused this time by a nested struct that contains pointers.
The macro is doing one thing. It casts the hook pointer to char *, subtracts offsetof(struct task, tasks), which is 24, and casts the result back to struct task *. found and &t are the same address, so found->pid is 42.
If tasks were the first field, a plain cast to struct task * would happen to work, because the offset would be 0. I checked that too. It fails as soon as you put any field in front of the hook. The real task_struct has many fields before tasks, so a cast would point at the wrong place. container_of does not need the inner struct to be first.
The Linux kernel's own container_of lives in include/linux/container_of.h. It does the same subtraction, and it also checks that the pointer you pass has the same type as the member. Recent kernel headers prefer container_of_const in new code, which keeps a const pointer const. The arithmetic is the same.
The kernel often wraps this in list_entry, which is container_of with a name that reads well when you are following a list. Walking those lists is a topic of its own. I wrote about that in Linked Lists Explained with Real Examples from the Linux Kernel. The idea to keep from this post is the layout: the hook lives inside the object, and container_of is how you get the object back.
Practice exercises
Try these on the same kind of machine, gcc on 64-bit x86, and check your answers with sizeof and offsetof. In PowerShell with MinGW, run the program with .\name.exe.
- Predict the size and the field offsets of this struct, then compile a tiny program to check:
struct clock {
char running;
struct {
int sec;
int nsec;
} t;
};
You should see a size of 12, running at 0, t at 4, t.sec at 4 and t.nsec at 8. Can you explain the 3 bytes after running?
-
Using the
struct studentandstruct addressfrom this post, write a designated initialiser for a student named "Meera", roll number 3, marks 91, living in "Surat" with pin 395007. Print the nested fields to check. -
What is
sizeof(struct node)below, and at what offset doeshookstart? Then make astruct node n, setn.idto 7, and get back tonfrom&n.hookwithcontainer_of. Printfound->idto prove it worked.
struct list_head {
struct list_head *next;
struct list_head *prev;
};
struct node {
int id;
struct list_head hook;
char tag;
};
You should see a size of 32, hook at offset 8, and tag at 24. Why is the struct 32 bytes, not 4 + 16 + 1 = 21?
- Take
struct outerfrom earlier in the post. Add a new first field,long key, in front offlag, and print the new offsets offlag,nestandid. Didneststill start at 4? If not, why did it move?
What comes next
We now know how to nest one struct in another, how the inner block is padded, how to reach nested fields with a dot or the arrow operator, and how the Linux kernel uses container_of to get from an embedded list_head back to a task_struct.
In the next post, Pointers to Structs and the Arrow Operator, we will look at how the Linux kernel links structs by pointer instead of storing a full copy inside, and when each choice makes sense.
If you want to practice this with small exercises that check your code as you go, the C programming course at Jupius Learning covers structs and memory step by step.
Frequently asked questions
Can a C struct contain another struct?
Yes. Declare the inner type, then use it as a field of the outer type, for example struct address addr inside struct student. The inner struct is stored inside the outer one. You can also write the inner struct inline, at the same time as you declare the field.
How do you access a field of a nested struct in C?
Use a dot for each step when you have a struct variable, like s1.addr.pin. When you have a pointer to the outer struct, use the arrow operator for the first step, like p->addr.pin. You can also take a pointer to the inner struct with &s1.addr and then write ap->pin.
Why is a struct with a nested struct bigger than the sum of its fields?
The inner struct may contain padding of its own, and the outer struct may add padding before or after that block so the inner struct starts at a correctly aligned offset. In this post, struct inner is 8 bytes with a 3 byte hole, and struct outer is 16 bytes because it also leaves 3 bytes of padding before nest.
What does container_of do in the Linux kernel?
It takes a pointer to a field inside a struct and returns a pointer to the struct that contains it. It works by subtracting offsetof(type, member) from the field pointer. The kernel uses it when a generic hook such as list_head is embedded in a larger struct, for example to get a task_struct from its tasks field.
Does the nested struct have to be the first field for container_of to work?
No. If the member is at offset 0, a plain cast happens to work as well. container_of also works when the member sits later in the struct, which is the usual case in the Linux kernel. In the struct task example in this post, tasks starts at offset 24.