# Null terminated array of strings from C function

**URL:** <https://ziggit.dev/t/null-terminated-array-of-strings-from-c-function/2459>\
**Category:** Help\
**Tags:** language, c\
**Created:** [December 14, 2023, 2:23pm UTC](https://ziggit.dev/t/null-terminated-array-of-strings-from-c-function/2459 "2023-12-14T14:23:13Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![castholm](https://ziggit.dev/user_avatar/ziggit.dev/castholm/32/1330_2.png) [@castholm](https://ziggit.dev/u/castholm)\
**Post date:** [December 14, 2023, 4:22pm UTC](https://ziggit.dev/t/null-terminated-array-of-strings-from-c-function/2459/4 "2023-12-14T16:22:50Z")

</div>

> 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`](https://ziglang.org/documentation/master/std/#A;std:mem.span) to convert it to a slice:

```zig
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.)

---

_[View the full topic](https://ziggit.dev/t/null-terminated-array-of-strings-from-c-function/2459)._
