Understanding Forward Declarations in C

One of the best ways to learn systems programming is by reading real-world code. Recently, while reading the Linux kernel source, I noticed something interesting inside drivers/pinctrl/core.h: #include <linux/kref.h> #include <linux/list.h> #include <linux/mutex.h> #include <linux/radix-tree.h> #include <linux/types.h> #include <linux/pinctrl/machine.h> struct dentry; struct device; struct device_node; struct module; At first glance, these lines seem incomplete. We see declarations like struct device; but no actual definition. Where is the complete structure with all its members? Why didn’t file defining this structure isn’t included? ...

May 17, 2026 · 6 min · Ankit

C Memory Layout: Structs, Unions, and a Linux Kernel Patch

Overview C memory layout looks simple in isolation: structs store fields sequentially, unions overlap memory, and flexible array members allow variable-sized trailing storage. But combining these features can create subtle layout issues. While working on a Linux kernel selftest, I encountered a case where a union contained multiple structures, each ending with a flexible array member at different offsets. Although the code was functionally correct, this confused the compiler into treating the union as effectively variable-sized, triggering a layout warning. ...

May 4, 2026 · 11 min · Ankit