Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?
like image 890
sfelber Avatar asked Sep 18 '14 17:09

sfelber


People also ask

Do parentheses work in C?

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

Which operator has higher precedence in 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.

What is the priority of operators* and in C?

Precedence of operators In C, the precedence of * is higher than - and = .


1 Answers

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.

like image 176
haccks Avatar answered Oct 25 '22 21:10

haccks