How to pass argv to std.process.Child.init?

Here’s a minimal working example of the error I’m facing

$ cat run_rick_roll.zig
const std = @import("std");
const log = std.log;

pub fn main() !void {
    var tsa = std.heap.ThreadSafeAllocator{ .child_allocator = std.heap.c_allocator };
    const a = tsa.allocator();
    log.info("Spawning rick roll child process", .{});
    const child = std.process.Child.init(&[_][]u8{ "/bin/bash", "roll.sh" }, a);
    const exit_code = try child.spawnAndWait();

    log.info("Rick rolled with exit code {:}", .{exit_code});
}

This doesn’t compile as expected due to this line

const child = std.process.Child.init(&[_][]u8{ "/bin/bash", "roll.sh" }, a);

Here’s the error

$ zig build-exe -lc run_rick_roll.zig
run_rick_roll.zig:8:52: error: expected type '[]u8', found '*const [9:0]u8'
    const child = std.process.Child.init(&[_][]u8{ "/bin/bash", "roll.sh" }, a);
                                                   ^~~~~~~~~~~
run_rick_roll.zig:8:52: note: cast discards const qualifier
referenced by:
    callMain: /home/cocoa/.local/bin/zig-linux-x86_64-0.13.0/lib/std/start.zig:524:32
    callMainWithArgs: /home/cocoa/.local/bin/zig-linux-x86_64-0.13.0/lib/std/start.zig:482:12
    remaining reference traces hidden; use '-freference-trace' to see all reference traces

Can anybody help me understand how to pass the argument vector?

string literals are const as they are located in read only memory, ie change the type to &[_][]const u8{...}
fyi you can replace types with . when initialising to get zig to infer the type ie &.{...}

2 Likes