How to print [*]const u8

So, i got the following function:

fn getRequiredExtensions(allocator: std.mem.Allocator) [][*]const u8 {
    var glfwExtensionCount: u32 = 0;
    var glfwExtensions: [*]const [*]const u8 = @ptrCast(c.glfwGetRequiredInstanceExtensions(&glfwExtensionCount));

    var extensions = std.ArrayList([*]const u8).init(allocator);
    errdefer extensions.deinit();

    // TODO: Handle this error better.
    // Error is OutOfMemory, so almost unreachable
    extensions.appendSlice(glfwExtensions[0..glfwExtensionCount]) catch unreachable;

    if (g_enableValidationLayers) {
        // TODO: Same as above
        extensions.append(c.VK_EXT_DEBUG_REPORT_EXTENSION_NAME) catch unreachable;

    logger.debug("Vulkan required extensions: {d}", .{glfwExtensionCount});
    for (extensions.items) |value| {
        logger.debug("  {s}", .{value});

    return extensions.toOwnedSlice() catch unreachable;

In the last loop, when trying to print the required extensions I get the following compile error:

error: invalid type given to std.mem.span: [*]const u8

I think i kind of know what it means but not sure. Most of the code it’s from a vulkan example I found, so it is possible the problem is in how I get the extensions and not how to print them.

Also, any improvement to the code would be appriciated.

The {s} format specifier is trying to use std.mem.span to convert the pointer to a slice, but you can only use that function with [*c] C pointers or [*:0] sentinel-terminated many-pointers.

If glfwGetRequiredInstanceExtensions returns an array of C-strings, the correct type to coerce the result to is [*]const [*:0]const u8. Those [*:0]const u8 strings will be able to be formatted using the {s} specifier.

If you are going to use these strings beyond just printing them in a loop it might be a good idea to immediately slice them using std.mem.span before adding them to an array list of type std.ArrayList([:0]const u8).

1 Like

yep, it worked, that is what happens when you steal code and don’t know what you are doing hehe

1 Like

don’t worry even copilot made this mistake on my code today :wink: