Hi. I’m trying to use a sentinel-terminated array as a stack or such. I want to do something like this:
var list: [20:0xffff]u16 = .{}; // sentinel-terminated array with 0xffff in index 0
That does not work, the compiler error is error: expected 20 array elements; found 0
, which makes sense.
But then if I do this:
const print = @import("std").debug.print;
pub fn main() !void {
var list: [20:0xffff]u16 = .{0xffff} ** 20;
print("Len {}, ", .{list.len});
list[0] = 1;
for (list) |x| {
print("{x} ", .{x});
}
}
The program prints
Len 20, 1 ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff
.
This makes little sense, because Zig documentation states that
The syntax
[N:x]T
describes an array which has a sentinel element of valuex
at the index corresponding to the lengthN
.