but I have no idea how you’d be running into this scenario when working with C code, and in fact [2053]u8 as a parameter is not allowed for functions with the .c calling convention, so I’m doubly confused.
I think one of the most important things is to understand the difference between arrays and pointers in Zig.
In Zig, arrays are value types, not pointer types. This means that e.g. [5]u8 takes up 5 actual bytes of memory, and, in your example, [2053]u8 is a value made up of 2053 bytes of memory.
When working with C from Zig, my general advice would be to use sentinel-terminated slices on the Zig side (e.g. [:0]u8 or [:0]const u8; many standard library functions will have a Z suffixed function that will give you a sentinel-terminated result, e.g. std.fs.path.joinZ, etc) and then you can pass those as sentinel-terminated pointers when calling into C functions that expect null-termination. Note that string literals are already pointers to sentinel terminated arrays (*const [N:0]u8), so you can pass them directly to C functions without issue (e.g. your_c_function("foo");).
When I do translate-c, I can observer that the C code is converted to struct where the filename has type [2053]u8 (I guess it will be converted to [*c]u8 before executing the function.
I tried just passing the pointed (.ptr) to the field, but I still get an error:
error: expected type '[2053]u8', found '[*]const u8'
image_info.*.filename = filename.ptr;
It sounds like it should be a simple task, but I’ve spent a whole day trying to figure this out…
I assume this line is ultimately what you’re trying to translate to Zig:
strcpy(imageInfo->filename, infile);
It is copying the bytes from infile into imageInfo->filename (including the null terminator). The equivalent in Zig would be to use @memcpy:
// Copy the bytes
// @memcpy requires the dest and src lengths to match, so we
// slice the filename field to the length of `filename`
@memcpy(&image_info.filename[0..filename.len], filename);
// Write the null terminator
image_info.filename[filename.len] = 0;
See what I said about arrays in this comment for some more explanation.
However, the first comment did not help me. I know what arrays are, I know how items are stored in arrays. That’s not where I have the problem. I am struggling with zig way of doing things at times. For example, how I can assign from one type to another similar type.
It’s a bit counterintuitive, but I would say that [2053]u8 and [*]const u8 are not actually similar types. To put it another way, how to convert between them is similar to how you’d go about converting between something like an integer type and [*]const u8.
translate-c is really only intended to be used with header files. Using it as a general C → Zig converter will give you entirely unreadable code, as you’ve seen.