liveBook · Manning

Multi-item pointer

As explained in Systems Programming with Zig

A many-item pointer is a Zig pointer type that represents the address of an unspecified number of contiguous elements without storing their length.

A many-item pointer, written `[*]T` or `[*]const T`, points to contiguous elements without recording how many there are. It can be indexed, but the programmer must track the valid length. section 2.2.3

Choose between a many-item pointer and a slice based on what the data needs:

  • A slice also carries a length and provides safety checks; a many-item pointer avoids storing the length, which can suit optimized data structures. section 2.2.3
  • Single-item pointers to arrays can coerce to many-item pointers. In C integration, many-item pointers can express the meaning of pointers to multiple elements. section 2.2.3
Listing 2.10 Slices
const std = @import("std");

pub fn main() void {
    const arr = [_]i32{ 11, 22, 33, 44, 55 };    #1
    const slice: []const i32 = &arr;    #2
    const x = slice[2];    #3

    std.debug.print("slice: {any}, x: {}\n", .{slice, x});
    std.debug.print("ptr: {*}, len: {}\n", .{slice.ptr, slice.len});
}

This slice example displays its pointer and length, showing the information a slice carries alongside its elements. section 2.2.3

How Systems Programming with Zig covers it

How deep it goes
Defines syntax, indexing, array-pointer coercion, and slice trade-offs, while deferring practical C integration.
Examples
Displays a slice’s pointer and length and describes array-pointer coercion.
What you can do after
Apply it to index contiguous elements while tracking valid length separately.
How it connects
Links many-item pointers to slices, arrays, and C pointers.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Systems Programming with Zig. The section links point to the parts of the book it draws on.