Chinaunix

标题: 强制类型转换的优先级是多少? [打印本页]

作者: maxxfire    时间: 2008-09-18 15:15
标题: 强制类型转换的优先级是多少?
强制类型转换它的优先级排多少?

比如:
(Type*)p->a;

(Type*)是对p做,还是对a做?
作者: nicozhou    时间: 2008-09-18 15:17
只对a做
作者: maxxfire    时间: 2008-09-18 15:42
为了安全点,还是这样写:
(Type*)(&p->a);
作者: cugb_cat    时间: 2008-09-18 15:44
不清楚优先级的,就加括号。。
作者: jinmiaobis    时间: 2008-09-18 15:45
还是按照你想要的结果加括号把,记优先级,我是记不住了.
作者: Godbach    时间: 2008-09-18 15:53
原帖由 cugb_cat 于 2008-9-18 15:44 发表
不清楚优先级的,就加括号。。


偶一般都是这样的,记住括号的优先级啊,哈哈
作者: zszjxh    时间: 2008-09-18 16:02
我不喜欢加括号

[ 本帖最后由 zszjxh 于 2008-9-18 16:06 编辑 ]
作者: emacsnw    时间: 2008-09-18 16:07
原帖由 zszjxh 于 2008-9-18 00:02 发表
我不喜欢加括号


我也是,括号太多影响程序美观。。。
作者: nicozhou    时间: 2008-09-18 16:08
原帖由 emacsnw 于 2008-9-18 16:07 发表


我也是,括号太多影响程序美观。。。


作践自己也作践别人
作者: langue    时间: 2008-09-18 20:47
原帖由 Godbach 于 2008-9-18 15:53 发表


偶一般都是这样的,记住括号的优先级啊,哈哈


括号的优先级是最低的。
作者: dodolo0k    时间: 2008-09-18 21:48
原帖由 Godbach 于 2008-9-18 15:53 发表


偶一般都是这样的,记住括号的优先级啊,哈哈

普通括号优先级最高级(与-〉同级 ),且是从左到右结合 ,但强制类型转换的括号优先级是次高级
作者: tyc611    时间: 2008-09-18 22:11
原帖由 langue 于 2008-9-18 20:47 发表


括号的优先级是最低的。


作者: langue    时间: 2008-09-18 23:06
原帖由 tyc611 于 2008-9-18 22:11 发表



错了,是最高的……

http://www.difranco.net/cop2220/op-prec.htm
作者: emacsnw    时间: 2008-09-19 01:58
原帖由 nicozhou 于 2008-9-18 00:08 发表


作践自己也作践别人


while (*p++ = *q++) 就很好,非要写成 while (*(p++) = *(q++)) 吗?
或者 if (a == b && c != d) 非要写成 if ((a == b) && (c != d)) ?
作者: mzli    时间: 2008-09-19 06:56
原帖由 emacsnw 于 2008-9-19 01:58 发表


while (*p++ = *q++) 就很好,非要写成 while (*(p++) = *(q++)) 吗?
或者 if (a == b && c != d) 非要写成 if ((a == b) && (c != d)) ?


此人活杠头,照你这种理论,我a=b+c*d+e*f; 要写成啥样?那么明显的玩意用括号,真是闲的难受。看好别人的意思发言好不好?拜托了。谢谢。

while (*p++ = *q++) 就很好,非要写成 while (*(p++) = *(q++))


啥条件语句?搞清楚一个两个的关系。==?

[ 本帖最后由 mzli 于 2008-9-19 06:57 编辑 ]
作者: jronald    时间: 2008-09-19 08:31
其实仔细想想就知道了,标准也是这么定出来的。
作者: nicozhou    时间: 2008-09-19 08:50
原帖由 emacsnw 于 2008-9-19 01:58 发表


while (*p++ = *q++) 就很好,非要写成 while (*(p++) = *(q++)) 吗?
或者 if (a == b && c != d) 非要写成 if ((a == b) && (c != d)) ?


我要投诉你灌水。

另外,你好好去学学编程规范。
作者: emacsnw    时间: 2008-09-19 09:59
原帖由 nicozhou 于 2008-9-18 16:50 发表


我要投诉你灌水。

另外,你好好去学学编程规范。


ft,我灌什么水了?我只是表达了我的看法:过多的括号会影响程序美观。
我又没说该加括号以便更清楚的时候不加。
作者: drunkedcat    时间: 2008-09-19 12:35
又是括号。。。。。
清楚明白的时候就不用,容易误会的时候就用。
作者: maxxfire    时间: 2008-09-19 13:45
  
Operator
  
  
Description
  
  
Associativity
  
  
()
  []
  .
  ->
  ++  --
  
  
Parentheses (function call) (see Note 1)
  Brackets (array subscript)
  Member selection via object name
  Member selection via pointer
  Postfix increment/decrement (see Note 2)
  
  
left-to-right
  
  
++  --
  +  -
  !  ~
  (type)
  *
  &
  sizeof
  
  
  
Prefix increment/decrement
  Unary plus/minus
  Logical negation/bitwise complement
  Cast (change type)
  Dereference
  Address
  Determine size in bytes
  
  
right-to-left
  
  
*  /  %
  
  
Multiplication/division/modulus
  
  
left-to-right
  
  
+  -
  
  
Addition/subtraction
  
  
left-to-right
  
  
<<  >>
  
  
Bitwise shift left, Bitwise shift right
  
  
left-to-right
  
  
<  <=
  >  >=
  
  
Relational less than/less than or equal to
  Relational greater than/greater than or equal to
  
  
left-to-right
  
  
==  !=
  
  
Relational is equal to/is not equal to
  
  
left-to-right
  
  
&
  
  
Bitwise AND
  
  
left-to-right
  
  
^
  
  
Bitwise exclusive OR
  
  
left-to-right
  
  
|
  
  
Bitwise inclusive OR
  
  
left-to-right
  
  
&&
  
  
Logical AND
  
  
left-to-right
  
  
||
  
  
Logical OR
  
  
left-to-right
  
  
?:
  
  
Ternary conditional
  
  
right-to-left
  
  
=
  +=  -=
  *=  /=
  %=  &=
  ^=  |=
  <<=  >>=
  
  
Assignment
  Addition/subtraction assignment
  Multiplication/division assignment
  Modulus/bitwise AND assignment
  Bitwise exclusive/inclusive OR assignment
  Bitwise shift left/right assignment
  
  
right-to-left
  
  
,
  
  
Comma (separate expressions)
  
  
left-to-right
  

作者: maxxfire    时间: 2008-09-19 13:45
上表从上到下,优先级由高到低。。
另外还有两个NOTE:
Note 1:
    Parentheses are also used to group expressions to force a different order of evaluation; such parenthetical expressions can be nested and are evaluated from inner to outer
Note 2:
    Postfix increment/decrement have high precedence, but the actual increment or decrement of the operand is delayed (to be accomplished sometime before the statement completes execution). So in the statement  y = x * z++; the current value of z is used to evaluate the expression (i.e., z++ evaluates to z) and z only incremented after all else is done.

[ 本帖最后由 maxxfire 于 2008-9-19 13:47 编辑 ]
作者: crunchyou    时间: 2008-10-22 18:04
强制类型转换可以看作一元操作符, 优先级与这些一元操作符相当:!(逻辑非) .(位取反) -(负号) ++(加1) --(减1) &(变量地址)
作者: crunchyou    时间: 2008-10-22 18:06
强制类型转换可以看作一元操作符, 优先级与这些一元操作符相当:!(逻辑非) .(位取反) -(负号) ++(加1) --(减1) &(变量地址)
作者: tianruoqiwo    时间: 2011-11-03 10:38
(Type*)p->a;

void * pData;//pData是存在的一块儿已知内存

((TYpe*)pData)->a;
这样就可以对任意指针 扣上任意结构体的形式,还能直接赋值
作者: chinesedragon    时间: 2011-11-03 12:34
呵呵,看来又成水了




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2