Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Precedence between member access from a pointer and cast

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

like image 557
misteryes Avatar asked Feb 17 '23 06:02

misteryes


2 Answers

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
=============   ===========                                     =============
like image 170
jxh Avatar answered Mar 04 '23 10:03

jxh


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

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

like image 43
vvy Avatar answered Mar 04 '23 11:03

vvy