#[solved] Which []u8 type is the most compatible with C?
1 messages · Page 1 of 1 (latest)
[*c]u8
Other than that, it depends on the context. For a C string ?[*:0]u8
there is no one answer to this question, since the semantics of []u8 dont directly exist in C, can you provide a code sample of what the C side looks like?
passing const thing = ""; to C where a const char* is expected
just pass "", ittl coerce
but I need to pass the string around my zig code
then [:0]u8
[:0]u8 should be fine. Then pass the .ptr property to C
wouldn't it just be
struct foo {
char *ptr;
size_t len;
}
(not sure)
no
the .ptr idea might be the way to go, yeah
[]u8 has no set memory layout, the compiler is free to lay it out in any way
.ptr on [:0]u8 returns [*:0]u8, which should freely coerce to a [*c]u8
✍️
in the future, this will only be usable in auto-translated files: https://github.com/ziglang/zig/issues/2984
if you're writing code by hand, any of the other pointer types are preferred, e.g. [*:0]u8 if you're using the C ABI and it needs to be null-terminated
pub const A = [:0]const u8;
pub const B = [*:0]const u8;
btw, what is the difference from Zig's code from using these two?
is it reasonable to use B in general, or is there any reason to avoid it and prefer A?
Top has length information
Bottom have to iterate through the values it points to to find lenght
ah, i see. definitely A for the zig code then, tyty