开发者

Are parenthesis needed to get the address of a struct member from a pointer to the struct as in "&(s->var)" vs "&s->var"?

开发者 https://www.devze.com 2023-02-28 14:03 出处:网络
I have a struct str *s; Let var be a variable in s. Is &s->var equal 开发者_C百科to &(s->var)?Behavior-wise, yes they are equivalent since the member access -> operator has a higher

I have a struct str *s;

Let var be a variable in s. Is &s->var equal 开发者_C百科to &(s->var)?


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.


Yes.

(-> is higher precedence than &. See http://cppreference.com/wiki/language/operator_precedence)


Yes, because the pointer dereference operator -> has higher precedence than the address operator &.

  • C operator precedence
0

精彩评论

暂无评论...
验证码 换一张
取 消