Precedence between member access from a pointer and cast

陌路散爱 提交于 2019-12-22 11:24:25

问题


If I have

 typedef struct{
       int i;
 } typeB;

 typeA *p;

then: What is the precedence between member access from a pointer and cast?

 (typeB *)p->i  

Is it actually ((typeB *)p)->i or (typeB *)(p->i)?


回答1:


An operator precedence table would show that a -> binds more tightly than the cast.

typedef void *typeA;
typeB b;
typeA a = &b;
(typeB *)a->i;   /* wrong: attempt to dereference a void pointer */
((typeB *)a)->i; /* ok */

A complete operator precedence table for your future reference is provided below. In the table, operators higher in the list bind more tightly than operators lower in the list (so the Primary Expression Operators bind the most tightly). If operators of the same precedence are used in the same expression in an ambiguous way (that is, not captured within a Primary Expression Operator like () or []), then it is resolved by following the associativity direction. So, for example the expression:

7 + (3 - 5 * 2 + 15) - 6

is evaluated in this order (according to the table):

7 + (3 - 10 + 15) - 6     // evaluate * in ()
7 + (-7 + 15) - 6         // evaluate - in () (it is left of the +)
7 + 8 - 6                 // evaluate + in ()
15 - 6                    // evaluate + (it is left of the -)
9                         // evaluate -

Of course, the compiler is free to perform the computation differently, but its result must match the result obtained when following the precedence rules.

Operator Type   Operator(s)                                     Associativity
=============   ===========                                     =============
Primary         () [] . -> expr++ expr--                        left-to-right
Expression 
Operators       
-------------   -----------                                     -------------
Unary           * & + - ! ~ ++expr --expr (typecast) sizeof     right-to-left
Operators       
-------------   -----------                                     -------------
Binary          * / %                                           left-to-right
Operators       + -
                >> <<
                < > <= >=
                == !=
                &
                ^
                |
                &&
                ||
-------------   -----------                                     -------------
Ternary         ?:                                              right-to-left
Operator
-------------   -----------                                     -------------
Assignment      = += -= *= /= %= >>= <<= &= ^= |=               right-to-left
Operators       
-------------   -----------                                     -------------
Comma           ,                                               left-to-right
=============   ===========                                     =============



回答2:


in C, -> operatior's precedence is higher than cast , which is (type name)

so (typeB *)p->i is (typeB *)(p->i)



来源:https://stackoverflow.com/questions/17077628/precedence-between-member-access-from-a-pointer-and-cast

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!