C Pointers, Strings, and Types
Bridge C and Zig type systems.
C Pointers, Strings, and Types is a free Zig Academy lesson on CoddyKit — lesson 3 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.
C Types Get Translated
When Zig imports C, each C type maps to a Zig one. An int becomes c_int, so widths match the C ABI exactly.
const x: c_int = 10;The C ABI Integers
Zig provides ABI-matching names like c_int, c_uint, c_long, and c_char so your values line up with what C expects.
C Pointers Use [*c]
A raw C pointer becomes the special [*c]T type. It can be null and converts loosely, mirroring C's flexible pointers.
var p: [*c]u8 = null;Why [*c] Is Special
The [*c] type bridges C's any-length, maybe-null pointers. It coerces to and from Zig slices and pointers where it can.
C Strings Are Bytes
A C string is just a pointer to bytes ending in a zero. There is no length stored, only the null terminator.
Null-Terminated Literals
Write a C-style string with the sentinel form. The type [*:0]const u8 marks bytes that end in a 0.
const s: [*:0]const u8 = "hello";Pass Strings to C
Zig string literals are already null-terminated, so you can hand them straight to C functions expecting a char pointer.
c.puts("done");Read a C String Back
To turn a C string into a Zig slice, use std.mem.span, which walks to the null byte to find the length.
const slice = std.mem.span(cstr);void Pointers
C's void* shows up as ?*anyopaque in Zig. You cast it back to a concrete pointer type before using it.
Cast with @ptrCast
Bridging pointer types across the C boundary uses @ptrCast, which reinterprets a pointer without changing the address.
const p: *Node = @ptrCast(@alignCast(raw));Match, Do Not Guess
The safe habit is to match C's exact types: use c_int for int and span for strings, so nothing is silently wrong.
Quick Check
Recall how Zig turns a C string into a usable slice.
Recap
You learned that C types map to ABI names like c_int, C pointers use [*c], and std.mem.span turns C strings into slices. 🎯
Frequently asked questions
Is the “C Pointers, Strings, and Types” lesson free?
Yes — the full text of “C Pointers, Strings, and Types” 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 “C Pointers, Strings, and Types”?
Bridge C and Zig type systems. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “C Pointers, Strings, and Types” 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
- Import C Headers with @cImport
- Calling C Functions and Linking
- C Pointers, Strings, and Types
- zig translate-c to Read C as Zig