I have a struct str *s
;
Let var
be a variable in s
. Is &s->var
equal to &(s->var)
?
In C, we can get the memory address of any variable or member field (of struct). To do so, we use the address of (&) operator, the %p specifier to print it and a casting of (void*) on the address.
Pointers to Structures Like we have pointers for int, char and other data-types, we also have pointers pointing to structures.
Self Referential structures are those structures that have one or more pointers which point to the same type of structure, as their member. In other words, structures pointing to the same type of structures are self-referential in nature.
Although a structure cannot contain an instance of its own type, it can can contain a pointer to another structure of its own type, or even to itself.
Behavior-wise, yes they are equivalent since the member access ->
operator has a higher precedence than the address-of &
operator.
Readibility-wise, the second one &(s->var)
is much more readable than &s->var
and should be preferred over the first form. With the second form, &(s->var)
, you won't have to second-guess what it's actually doing as you know the expression in the parentheses are always evaluated first. When in doubt, use parentheses.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With