How to return a C string from []u8

Hi @jailop, welcome to ziggit!

I edited your post, I think you meant []u8?

I think you are looking for std.mem.Allocator.dupeZ to convert []u8 to [:0]u8 and then you can use .ptr on that to pass it to C.

const c_str = try allocator.dupeZ(str);
defer allocator.free(c_str);
c_function(c_str.ptr);

Also take a look at this, for more details:

1 Like