c++coperator-precedenceampersandaddressof

Using C ampersand with or without brackets


Let's have two lines of code:

&car->speed
&(car->speed)
  1. Are these two lines equivalent? Will I get in both cases address to the speed?
  2. If they are equivalents, what is better to choose as coding convention?

Solution

  • 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.