#Struct member access?
1 messages · Page 1 of 1 (latest)
findTable
+--------+--------+
| header | tables |
+--------+--------+
~~~~~~~~~~~~~~~ reading this
findTable1
+--------+--------+
| header | tables |
+--------+--------+
|
|
| +-------------+
+----> | ... ... ... |
+-------------+
~~~~~~~~~~~~~~~ reading this
if findTable (no 1) is the working version - you have done your type-modelling wrong.
"type modelling" as in, writing Zig types that represent the meaning of some sequence of bytes, according to some format
the tables field in the first code snippet is wrong - it represents a pointer to some remote data; when you actually want to represent a sequence of inline elements, in the classic header-body format
Zig does not have flexible array members, I suggest you completely remove the tables field and instead make a utility method:
const LengthPrefixedString = extern struct {
len: u64,
body: void,
fn getSlice(self: *const LengthPrefixedString) []const u8 {
const ptr: [*]const u8 = @ptrCast(&self.body);
return ptr[0..self.len];
}
};
memory representation:
+--------+----+----+----+-/ /-+----+
| length | u8 | u8 | u8 | \ \ | u8 |
+--------+----+----+----+-/ /-+----+
|-------------------------| length given by the len field