Precedence between member access from a pointer and cast

前端 未结 2 761
不知归路
不知归路 2020-12-11 09:54

If I have

 typedef struct{
       int i;
 } typeB;

 typeA *p;

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

相关标签:
2条回答
  • 2020-12-11 09:59

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

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

    0 讨论(0)
  • 2020-12-11 10:04

    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
    =============   ===========                                     =============
    
    0 讨论(0)
提交回复
热议问题