0Pricing
Zig Academy · Lesson

Optional Pointers and ?*T

Combine pointers with optionality.

Optional Pointers and ?*T 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.

Pointers Can Be Optional Too

A pointer normally must always aim at a valid value. Make it optional and it gains a legitimate way to mean nothing yet.

The ?*T Shape

The type ?*T reads as: an optional pointer to a T. It either points at a real value or holds null.

var node: ?*Node = null;

A Safe Null Pointer

This is Zig answer to C null pointers. The pointer can be absent, but the optional forces you to check before dereferencing.

Unwrap Before You Follow

You cannot dereference a ?*T directly. First unwrap it with if or orelse to get the plain pointer underneath.

if (node) |p| {
    use(p.*);
}

Capture Gives a Real Pointer

Inside the if capture the name is a plain *Node, ready to dereference with .* like any non-optional pointer.

if (head) |p| {
    const value = p.*;
}

Great for Linked Structures

Optional pointers shine in linked lists and trees, where a next or child field is null at the end of the chain.

const Node = struct { next: ?*Node };

Null Marks the End

When you walk a list, a next field of null cleanly signals that you have reached the final node and should stop.

Zig Packs the Tag Away

For pointers, Zig stores the null state in the pointer value itself, so a ?*T takes no extra space over a plain pointer.

Const Optional Pointers

You can combine read-only and optional: ?*const T is an optional pointer through which you may read but never write.

var view: ?*const u8 = null;

Still No Surprise Crashes

Because the type says it might be null, the compiler will not let you forget the check, unlike a raw C pointer dereference.

Walking a Chain

A while loop that captures each optional next pointer is the idiomatic way to traverse a list until null appears.

while (cur) |n| {
    cur = n.next;
}

Quick Check

What does the type ?*Node describe?

Recap

You learned ?*T: a safe, optional pointer. Unwrap it before dereferencing, and let null mark the end of linked structures. 🔗

Frequently asked questions

Is the “Optional Pointers and ?*T” lesson free?

Yes — the full text of “Optional Pointers and ?*T” 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 “Optional Pointers and ?*T”?

Combine pointers with optionality. 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 “Optional Pointers and ?*T” 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

  1. Declaring Optional Types
  2. Unwrap Safely with if and orelse
  3. Optional Pointers and ?*T
  4. null Versus undefined
← Back to Zig Academy