Pointers

for Zig 0.15.2 Buy

Pointers

A pointer is a variable or constant whose value isn’t a direct piece of data, but a memory address. That’s why we say it “points” - it doesn’t hold the final value, but the location.

That address usually refers to a value such as a number, character, string, or boolean.

As an example, here n_x holds the number 42:

      const n_x: u8 = 42;

But in this other case:

      const n_x: *const u8 = &42;

If we print n_x, we’ll see something like: u8@11961b5. This means: “a value of type u8 stored (@ = at) at memory address 11961b5.”

The declaration where we write *const u8 means:

Keep in mind that const in front of n_x means that n_x is a constant value- so once it’s assigned, it can’t point to a different address, but it can point to either a constant or a variable.

If we want to access the value at the address the pointer is pointing to, we use .* after the name:

      print(“{}\n”, .{ n_x.* });

In our case, this will print 42.

Accessing the value stored at the pointer’s address is called dereferencing the pointer.

Here’s an example of a pointer pointing to the address of a variable:

ptr2var.zig

const std = @import("std");

const print = std.debug.print;

pub fn main() void {

    // define a variable

    var n_a: u8 = 42;

    // the pointer n_x is constant but points

    // to an address that holds a variable

    const n_x: *u8 = &n_a;

    // print the content at the address

    print("n_x: {}\n", .{n_x.*});

    // increment

    n_x.* += 1;

    // once again, print the content at the address

    print("n_x: {}\n", .{n_x.*});

}

$ zig run ptr2var.zig

n_x: 42

n_x: 43

Memory timing
String types
© 2025 - 2026 Zen of Zig