Let's have two lines of code:
&car->speed
&(car->speed)
speed
?Parentheses are used in C expressions in the same manner as in algebraic expressions. For example, to multiply a times the quantity b + c we write a * ( b + c ).
Certain operators have higher precedence than others; for example, the multiplication operator has a higher precedence than the addition operator. For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7.
Precedence of operators In C, the precedence of * is higher than - and = .
Are these two lines equivalent? Will I get in both cases address to the speed?
Yes. ->
has higher precedence than that of unary &
, therefore &car->speed
and &(car->speed)
are equivalent.
If they are equivalents, what is better to choose as coding convention?
Go with second as it shows the intended behaviour that you are interested in the address of speed
.
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