Skip to content

Zig Many-Item Pointers

A *T points at exactly one value. But what about a pointer to “some items in a row, and I don’t know how many”? That’s what C’s int * usually means, and Zig has a separate type for it: the many-item pointer, written [*]T. You won’t write many of these yourself. They mostly show up when you talk to C code, and in Zig code a slice does the same job more safely. This short lesson shows what they are, so they don’t confuse you when you meet one, and why you’ll almost always choose a slice instead.

You get a [*]T from a pointer to an array. Then you can index it like an array:

const std = @import("std");
pub fn main() void {
const numbers = [_]i32{ 10, 20, 30, 40 };
const many: [*]const i32 = &numbers;
std.debug.print("{d} {d}\n", .{ many[0], many[3] });
var p = many;
p += 1;
std.debug.print("after p += 1: {d}\n", .{p[0]});
p += 2;
std.debug.print("after p += 2: {d}\n", .{p[0]});
}

Output:

10 40
after p += 1: 20
after p += 2: 40

[*]const i32 reads as “pointer to an unknown number of i32 values that I won’t change”. &numbers is a *const [4]i32, and Zig turns it into a many-item pointer when you ask for one.

A many-item pointer also supports pointer arithmetic: p += 1 moves it one item forward (4 bytes for an i32, Zig does the multiplying). After that, p[0] is the item that used to be p[1]. This is the one thing a [*]T can do that a single-item *T can’t.

Here’s the catch. A many-item pointer is just an address. It has no idea how many items there are:

pub fn main() void {
const numbers = [_]i32{ 10, 20, 30, 40 };
const many: [*]const i32 = &numbers;
_ = many.len;
}
main.zig:4:13: error: type '[*]const i32' does not support field access
_ = many.len;
~~~~^~~~

And because it doesn’t know where the items end, you can’t loop over it with for either: Zig says unbounded for loop and type '[*]const i32' has no upper bound.

So the length has to travel separately, the way C functions do it, with a pointer and a count:

const std = @import("std");
fn sum(ptr: [*]const i32, len: usize) i32 {
var total: i32 = 0;
var i: usize = 0;
while (i < len) : (i += 1) {
total += ptr[i];
}
return total;
}
pub fn main() void {
const numbers = [_]i32{ 10, 20, 30, 40 };
std.debug.print("{d}\n", .{sum(&numbers, numbers.len)});
std.debug.print("{d}\n", .{sum(&numbers, 2)});
}

Output:

100
30

It works, but nothing ties ptr and len together. Pass the wrong len, and nobody notices.

With an array or a slice, a bad index stops your program with index out of bounds. A many-item pointer can’t check, because it doesn’t know the bounds:

const std = @import("std");
pub fn main() void {
const numbers = [_]i32{ 10, 20, 30, 40 };
const many: [*]const i32 = &numbers;
var i: usize = 6;
i += 0;
std.debug.print("{d}\n", .{many[i]});
}

When we ran this in a debug build, it printed 10, and in a fast build it printed a different number. There are only four items, so many[6] reads memory that belongs to something else. No error, no warning, just a wrong answer. Zig calls this illegal behavior: the program is broken, but nothing stops it. It’s the same class of bug as reading past the end of an array in C, and you’ll learn more about it in Zig Illegal Behavior.

A slice, []T, is exactly a many-item pointer plus a length, kept together in one value. Here’s the same sum with a slice:

const std = @import("std");
fn sum(items: []const i32) i32 {
var total: i32 = 0;
for (items) |n| total += n;
return total;
}
pub fn main() void {
const numbers = [_]i32{ 10, 20, 30, 40 };
std.debug.print("{d}\n", .{sum(&numbers)});
std.debug.print("{d}\n", .{sum(numbers[0..2])});
}

Output:

100
30

Shorter, a for loop works, the length can’t get lost, and every index is checked in safe builds. That’s why the language reference says Zig code generally prefers slices.

You can go back and forth. Slicing a many-item pointer with a start and an end gives you a slice (you’re the one promising the length is right), and every slice has a .ptr field that is its many-item pointer:

const std = @import("std");
pub fn main() void {
const numbers = [_]i32{ 10, 20, 30, 40 };
const many: [*]const i32 = &numbers;
const first_three = many[0..3];
std.debug.print("{} {any}\n", .{ @TypeOf(first_three), first_three });
var count: usize = 2;
count += 0;
const some = many[1 .. 1 + count];
std.debug.print("{} {any}\n", .{ @TypeOf(some), some });
const s: []const i32 = &numbers;
std.debug.print("{}\n", .{@TypeOf(s.ptr)});
}

Output:

*const [3]i32 { 10, 20, 30 }
[]const i32 { 20, 30 }
[*]const i32

When both ends are known while compiling (many[0..3]), you get a pointer to an array of exactly that size. When one end is only known at run time, you get a slice. Either way, from then on the length travels with the data. Turning a C-style pointer and length into a slice as early as possible is a good habit when you work with C libraries.

  • C libraries. A C function that takes const char * or int * shows up in Zig as a many-item pointer. You’ll pass slice.ptr and slice.len to it.
  • C strings. Text from C ends with a zero byte instead of carrying a length. Zig writes that as [*:0]const u8, a many-item pointer that knows how it ends. That’s the next lesson, Zig Sentinel-Terminated Types.
  • [*c]T. When Zig translates C headers automatically, pointers become “C pointers”, [*c]T, which can also be null. They’re only meant for C interop; the reference has a section on C pointers.
Type Points at Knows the length? Bounds checked?
*T exactly one item (one) no indexing
*[N]T an array of N items yes, N is in the type yes
[]T a slice: any number of items yes, .len at run time yes
[*]T an unknown number of items no no

Add const after the first part (*const T, []const T, [*]const T) to make it read-only.

This function counts zeros using a many-item pointer and a length. Rewrite it to take a slice []const u8, and call it with { 0, 3, 0, 0, 7 }.

fn countZeros(ptr: [*]const u8, len: usize) usize {
var count: usize = 0;
var i: usize = 0;
while (i < len) : (i += 1) {
if (ptr[i] == 0) count += 1;
}
return count;
}

Hint: with a slice, the while loop and the len parameter can go. A for loop does it.

Show solution
const std = @import("std");
fn countZeros(items: []const u8) usize {
var count: usize = 0;
for (items) |b| {
if (b == 0) count += 1;
}
return count;
}
pub fn main() void {
const data = [_]u8{ 0, 3, 0, 0, 7 };
std.debug.print("zeros: {d}\n", .{countZeros(&data)});
}

Output:

zeros: 3

One parameter instead of two, and there’s no way to pass a length that doesn’t match the data.

You get a [*]const u8 from “a C library” and are told it has 5 bytes. Make it into a slice and print it as text with {s}. Fake the C side with const from_c: [*]const u8 = "hello";.

Hint: from_c[0..5] gives you something with a length, which {s} needs.

Show solution
const std = @import("std");
pub fn main() void {
const from_c: [*]const u8 = "hello";
const text = from_c[0..5];
std.debug.print("{s} ({d} bytes)\n", .{ text, text.len });
}

Output:

hello (5 bytes)

Printing from_c with {s} directly wouldn’t work: print has no way to know where the text ends.

  • [*]T is a pointer to an unknown number of items. You get one from a pointer to an array, or from a slice’s .ptr.
  • You can index it and do pointer arithmetic (p += 1), but it has no .len and you can’t for over it.
  • There are no bounds checks: a bad index silently reads the wrong memory.
  • A slice []T is a pointer plus a length, so it’s safer and easier. Prefer slices in Zig code.
  • Slicing a [*]T (ptr[0..n]) turns it into a slice; do that as early as possible.
  • You’ll mostly meet [*]T and [*c]T when calling C.

Next up: Zig Slices, the type you’ll use for almost every list and string in Zig.

Quick check

1. What can a [*]i32 do that a *i32 cannot?
2. You index a [*]const u8 past the end of the real data. What happens in a debug build?
3. What is a slice, compared with a many-item pointer?