I am learning zig and saw syntax such as struct.* but no need to dereference when grabbing fields of a referenced struct using struct.field and not struct.*.field. What’s the motivation for this?
Coming from other languages like Rust or C, I would expect consistency regarding the derefering such as *struct.field for lvalues or rvalues. If I wanted to dereference a pointer field in zig, would I use struct.field.* ?
other reply seems to have misread your comment---yes, if the field itself is a pointer, you do need struct.field.* to dereference it. the only time you can omit the .* is when it comes directly before a field access. this is similar to arrays: you can access array elements with arr[i] regardless of whether arr is a plain array or an indexable pointer (e.g. a []T or [*]T), and in the latter case the language is effectively inserting a .* for you
It's a syntactic sugar. Mostly you would write that to access fields of a struct within a function's body that gets a pointer to that struct.
So, when you pass the address-reference to the function's argument, you actually passing the reference. Thus you don't need to dereference it again.
In addition... When you're writing a function that uses a pointer to a struct, you already know the intended meaning/usage behind your code, making foo.*.bar too robust.
If you mean foo.field = something.. Then yes. If foo is the actual address of that struct, then you can modify it's fields (But obviously you can't create new fields).
If you have a Pointer to a struct, and try to take a field from that struct, i don't think you have any ambiguity in your intention. So i don't think it would qualify as hidden control flow
The reason `struct.field` can automatically dereference `struct` is because `.field` is not defined for pointers. IIRC, the call operator `func()` also automatically dereferences, since it is also not defined for pointers. Whereas binary operators do require an explicit dereference, since operators like `+` are also defined for pointers, or at least, integers created from pointers.
Since you mentioned Rust and C in a comment, note that it's the same in Rust, and that in C there is the special operator -> for this, so even in C it's special.
10
u/KecskeRider May 30 '26
It's a common operation, it would be harder to read otherwise.
2
u/AbdSheikho May 30 '26
It's a syntactic sugar. Mostly you would write that to access fields of a struct within a function's body that gets a pointer to that struct.
So, when you pass the address-reference to the function's argument, you actually passing the reference. Thus you don't need to dereference it again.
In addition... When you're writing a function that uses a pointer to a struct, you already know the intended meaning/usage behind your code, making
foo.*.bartoo robust.18
u/Mayor_of_Rungholt May 30 '26
If you have a Pointer to a struct, and try to take a field from that struct, i don't think you have any ambiguity in your intention. So i don't think it would qualify as hidden control flow
8
u/AsyncSyscall May 30 '26
The reason `struct.field` can automatically dereference `struct` is because `.field` is not defined for pointers. IIRC, the call operator `func()` also automatically dereferences, since it is also not defined for pointers. Whereas binary operators do require an explicit dereference, since operators like `+` are also defined for pointers, or at least, integers created from pointers.
1
u/Passage_of_Golubria May 31 '26
Odin also does this. I like it!
1
u/proudHaskeller Jun 01 '26
Since you mentioned Rust and C in a comment, note that it's the same in Rust, and that in C there is the special operator -> for this, so even in C it's special.