0Pricing
Zig Academy · Lesson

Const Pointers and Alignment

Express read-only access and layout.

Const Pointers and Alignment 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.

Read-Only Access

Often you want to share a value but promise not to change it. A const pointer grants read access while forbidding writes.

The *const T Type

Write a pointer to immutable data as *const T. You can read through it, but assigning to its target is a compile error.

const x: i32 = 5;
const p: *const i32 = &x;

Const Travels With &

Taking the address of a const value yields a *const T automatically, so the immutability is preserved by the type.

const v: u8 = 9;
const p = &v; // *const u8

Mutable Coerces to Const

A *T coerces to a *const T freely, since dropping the write permission is always safe. The reverse is not allowed.

var n: i32 = 1;
const ro: *const i32 = &n;

Why Prefer const

Accepting a *const T parameter signals to callers that you will only read, which makes functions easier to trust.

fn show(p: *const i32) void {
    // read only
}

What Alignment Means

Every type has an alignment: the address boundary it must sit on. A u32 usually needs an address divisible by four.

Query with @alignOf

The builtin @alignOf(T) returns a type's required alignment in bytes, just as @sizeOf returns its size.

const a = @alignOf(u32);

Alignment in the Type

You can spell a pointer's alignment in its type using align, as in *align(4) u8, to demand a specific boundary.

var p: *align(4) u8 = undefined;

Why Alignment Matters

Hardware reads aligned data faster, and some CPUs fault on misaligned access. Zig tracks alignment in the type to keep this correct.

Over- and Under-Aligning

A pointer may promise stronger alignment than the default, but you cannot weaken it without an explicit cast like @alignCast.

Const and align Together

The two combine naturally: a *const align(8) T is a read-only pointer that also guarantees an eight-byte boundary.

Quick Check

You write a function that only needs to read the value behind a pointer. Which parameter type best expresses that?

Recap

A *const T pointer shares data read-only and coerces from *T, while alignment, seen via @alignOf and align(n), keeps memory access correct and fast. 🎯

Frequently asked questions

Is the “Const Pointers and Alignment” lesson free?

Yes — the full text of “Const Pointers and Alignment” 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 “Const Pointers and Alignment”?

Express read-only access and 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 “Const Pointers and Alignment” 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