Zig by Example
Zig by Example

Multi-Pointers

< 1 min read

Multi-Pointers

[*]T is a many-item pointer — basically a C pointer, with no length attached.

const std = @import("std");

pub fn main() void {
    var arr = [_]u32{ 10, 20, 30, 40 };

    // Coerce array to many-pointer
    const p: [*]u32 = &arr;

    std.debug.print("{d}\n", .{p[0]}); // 10
    std.debug.print("{d}\n", .{p[2]}); // 30

    // Arithmetic
    const p2 = p + 1;
    std.debug.print("{d}\n", .{p2[0]}); // 20

    // Convert back to a slice with known length
    const s: []u32 = p[0..arr.len];
    std.debug.print("len: {d}\n", .{s.len});
}

Reach for [*]T when you're talking to C APIs. In pure Zig code, prefer slices ([]T) — they carry their length around and rule out a whole class of bugs.

Zig by Example
Zig by Example

by Dariush Abbasi

A hands-on introduction to Zig via annotated examples. Covers the language from Hello World through memory allocation, concurrency, networking, and C interop.

View book →