extern struct and ABI Layout
Match C struct memory layout.
extern struct and ABI Layout is a free Zig Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Talking to C
When Zig calls into C code, both sides must agree on memory layout. That shared contract is the ABI.
Plain Structs Are Not Guaranteed
A regular struct lets Zig pick field order and padding freely, so its layout may not match what a C library expects.
Enter extern struct
Mark a type extern struct and Zig lays out its fields using the C ABI, matching a C struct field for field.
const Point = extern struct {
x: i32,
y: i32,
};Fields Stay in Order
An extern struct keeps your declaration order, just like C does, so the first field always comes first in memory.
Padding Like C
Unlike a packed struct, extern struct still adds padding for alignment, exactly the way a C compiler would.
Packed vs Extern
Use packed for bit-exact layout, and extern for matching C's byte layout. They solve different layout problems.
Pass Structs to C
An extern struct can be handed straight to a C function, because its layout matches the header the function was compiled against.
extern fn use_point(p: Point) void;Check the Size
Confirm an extern struct matches C with @sizeOf, which reports its total bytes including any alignment padding.
const n = @sizeOf(Point); // 8Inspect Field Offsets
Use @offsetOf to see exactly where each field starts, which is great for verifying a struct lines up with C.
const off = @offsetOf(Point, "y"); // 4Only C-Compatible Types
Fields in an extern struct must be C-compatible types, so Zig-only shapes like slices are not allowed inside one.
The Bridge to C
Together, extern struct and the C ABI let Zig and C share data safely with no copying or guessing about layout. 🔗
Quick Check
Recall what extern struct is designed to match.
Recap
You learned extern struct matches the C ABI: ordered fields with C-style padding, passable to C, verified with @sizeOf and @offsetOf. 🎉
Frequently asked questions
Is the “extern struct and ABI Layout” lesson free?
Yes — the full text of “extern struct and ABI Layout” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.
What will I learn in “extern struct and ABI Layout”?
Match C struct memory layout. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Zig Academy?
No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “extern struct and ABI Layout” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Zig Academy lesson?
Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- packed struct for Exact Layout
- Arbitrary-Width Integers like u3
- Bit Fields and Flags
- extern struct and ABI Layout