0Pricing
Zig Academy · Lesson

Dereferencing with ptr.*

Read and write through a pointer.

Dereferencing with ptr.* is a free Zig Academy lesson on CoddyKit — lesson 2 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.

Reaching the Value

A pointer holds an address, not a value. To use the value behind it you must dereference the pointer first.

The .* Syntax

Zig dereferences with a trailing .* on the pointer. So ptr.* means the value that ptr currently points to.

var x: i32 = 7;
const p = &x;
const v = p.*;

Read Through a Pointer

Writing ptr.* in an expression reads the pointed-to value, giving you a normal copy you can use anywhere.

const doubled = p.* * 2;

Write Through a Pointer

Put ptr.* on the left of an assignment to write a new value into the original location.

p.* = 99;

Changes Are Visible

Because the pointer shares the original storage, writing through it updates the variable everyone else sees too.

var x: i32 = 1;
const p = &x;
p.* = 5;
// x is now 5

No Arrow Operator

Zig has no -> like C. Whether you read or write a field, you always go through the same .* dereference.

Dereference Then Field

For a pointer to a struct, p.field works directly: Zig auto-dereferences for field access, so you skip the .* there.

const p = &point;
const y = p.y;

Explicit Is Allowed

You may still spell it out as p.*.field, but the shorter p.field is idiomatic Zig and reads more cleanly.

Const Blocks Writes

If the pointer is a *const T, then ptr.* is read-only; trying to assign through it is a compile error.

const x: i32 = 3;
const p = &x;
// p.* = 4; // error

Dereference in Math

You can use ptr.* anywhere a value fits, including arithmetic, so p.* + 1 just means the pointee plus one.

const next = p.* + 1;

A Common Mistake

Forgetting .* tries to use the address as a number, which is a type error. Zig catches it at compile time, not at runtime.

Quick Check

You have const p = &x and want to store a new number into x through p. What do you write?

Recap

Use ptr.* to read or write the value a pointer targets, struct fields auto-dereference, and *const T pointers stay read-only. 🎯

Frequently asked questions

Is the “Dereferencing with ptr.*” lesson free?

Yes — the full text of “Dereferencing with ptr.*” 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 “Dereferencing with ptr.*”?

Read and write through a pointer. 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 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Dereferencing with ptr.*” 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. Single-Item Pointers with *T
  2. Dereferencing with ptr.*
  3. Many-Item Pointers [*]T
  4. Const Pointers and Alignment
← Back to Zig Academy