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
