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

后端 未结 3 1034
伪装坚强ぢ
伪装坚强ぢ 2021-02-11 23:38

I have a struct str *s;

Let var be a variable in s. Is &s->var equal to &(s->var)?

3条回答
  •  没有蜡笔的小新
    2021-02-12 00:01

    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.

提交回复
热议问题