从指针访问成员和强制转换之间的优先级 [英] Precedence between member access from a pointer and cast

查看:23
本文介绍了从指针访问成员和强制转换之间的优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有

 typedef struct{
       int i;
 } typeB;

 typeA *p;

那么:从指针访问成员和强制转换之间的优先级是什么?

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

 (typeB *)p->i  

实际上是((typeB *)p)->i还是(typeB *)(p->i)?

推荐答案

运算符优先级表将显示 -> 的绑定比强制转换更紧密.

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
=============   ===========                                     =============

这篇关于从指针访问成员和强制转换之间的优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆