I am really new to zig, but so far language feels interesting.
In project I am using for learning zig I faced problem I don’t really know how to solve.
Basically, I have an array of some structs and I want to access raw bytes of it. Struct is packed, so such operation should not cause any problems, since every single byte of the array is meaningful.
To be more specific, I want smth like following
struct test {
uint8_t a;
uint8_t b;
uint8_t c;
uint8_t d;
};
struct test array[40];
((uint8_t *)array)[159] = 0x10;
I found that zig has std.mem.bytesAsSlice() function, but seems like it does not work the way I think. When I do
Not exactly. To force C ABI, you want extern struct. To remove padding, you need to add explicit align(1) to fields:
extern struct {
small: u8,
big: u64 align(1)
}
packed struct is different. It is a bitfield. That is, a packed struct is an integer in a fancy hat. It is much closer to Erlang BitSyntax than to packed from C.