Skip to content

Instantly share code, notes, and snippets.

@ljmccarthy
Created April 19, 2019 10:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ljmccarthy/248b3b269583e31f6e00adda44eac43e to your computer and use it in GitHub Desktop.
Save ljmccarthy/248b3b269583e31f6e00adda44eac43e to your computer and use it in GitHub Desktop.
fn cString(comptime str: []const u8) [*]const u8 {
return &(str ++ "\x00");
}
fn cStringArray(comptime strArray: [][]const u8) [*]const ?[*]const u8 {
comptime var out : [strArray.len + 1] ?[*]const u8 = undefined;
inline for (strArray) |str, i| {
out[i] = comptime cString(str);
}
out[strArray.len] = null;
return &out;
}
test "null terminated array of null terminated strings" {
var argv = cStringArray(&[][]const u8{
"hello",
"world",
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment