Suppose you have a C line of code &x->y->z
(doesn't matter what the x/y/z's are). What is the order of evaluation here? I see that ->
takes precedence over &
in https://en.cppreference.com/w/c/language/operator_precedence . So is this evaluation sequence correct?
x->y
is evaluated, and with that result (x->y)->z
is evaluated, and with that result &((x->y)->z)
is evaluated If this is incorrect, can you tell me what the correct order is?
You are right this is the correct order. explination below:
&x->y->z
if we look in your expression above no matter what is x,y and z. we see that its contained from arrow ->
operators or more technicaly speeking ( Structure and union member access through pointer
) and the address operator &
.
In c programing there is the precedence and associativity for all operators. when we come to expression that we have 2 operators with the same precedence then we need to look at the associativity inorder to know what expression we need to evaluate first.
your expression have 2 ->
operators and one &
operator. ->
operator is in the higest (precedence 1) precedence group of operators (he is not alone in this group see the table below). and &
in the second higest group (precedence 2) and he is not alone in this group. so operator ->
must be evaluated first. but where to start? here comes the associativity. for group 1 in the table below, we evaluates from left to right.
SO the order of the evaluation is:
x->y
: evaluate first. for example lets assume its evaluates to P
. (x->y=P) P->z
OR (x->y)->z
: evaluates after the first (we are going from left to right folowing the associativity rule). &((x->y)->z)
: only now we evaluate the &
operator and this will be the final result. see the link below for C Operator Precedence
You are exactly correct.
The ->
operator has the highest precedence and groups from left to right. So that gives you &((x->y)->z)
.
First of all, "precedence" doesn't mean order of evaluation. It refers to the association of operands with operators. For example in f() + g() * h()
, the three function calls could occur in any order; saying that *
has higher precedence is saying that the two operands of *
are g()
and h()
. The compiler might call all three functions, and then multiply the results of the g
and h
calls, and then add the result of the f
call.
In &x->y->z
things are easier because the second operand of ->
(and .
) isn't an expression. It's an identifier that names a class member. So the second operand isn't evaluated, really.
The precedence rules, or grammar table, tells use that the operand of the unary &
operator is x->y->z
.
To understand x->y->z
, clearly it is not the case that we are looking for a member of the struct pointed to by x
whose name is " y->z
" since that is not a valid identifier. That would not be a valid syntax parse. So it can only mean that ->z
is applied to the result of x->y
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.