Null terminated array of strings from C function

So, should I coerce it to the actual type ? Is it [*:0][*c]const u8 ?

The correct type of the returned list of ports is [*:null]?[*:0]const u8. Quite the mouthful.

As you discovered, for loops don’t work with sentinel-terminated pointers, only slices and arrays. But if you really wanted to use a for loop for whatever reason, you could use std.mem.span to convert it to a slice:

const ports: [*:null]?[*:0]const u8 = jack_get_ports(...);
for (std.mem.span(ports)) |port| {
    std.debug.print("{s}\n", .{port.?});
}

(In terms of actual work done this is strictly worse than the while loop since it will need to iterate through the list twice, first to compute the length to create the slice and then a second time to actually loop through it.)

5 Likes