我试图用新的编译器编译旧代码,并得到下一个错误:
error: cannot take the address of an rvalue of type 'int'
这是有两行的示例-一行编译,另一行给出错误
struct mstct {
int myfield;
int myfield2[5];
int myfield3[5];
};
typedef struct mstct db_data_px;
int foo(int a, int b, int c){
//the next code compiles successfully.
unsigned val1 = ((18 == c) ? ((unsigned) & (((db_data_px *) 0)->myfield)) : ((unsigned) & (((db_data_px *) 0)->myfield3[b]))); //successes
//the next code is failing
unsigned val2 = (unsigned) & ((18 == c) ? (((db_data_px *) 0)->myfield) : (((db_data_px *) 0)->myfield3[b]));
return 0; // failing
}
为什么第一行编译而第二行失败?为什么我需要在两个select表达式中都进行强制转换(无符号),并且仅在对select表达式求值后才进行强制转换?
在你的代码中
((18 == c) ? (((db_data_px *) 0)->myfield) : (((db_data_px *) 0)->myfield3[b]))
是不产生左值的条件表达式。
上面的表达式为您提供了一个右值(non-lvaue),您不能&
在该值上使用operator。
详细说C11
一下,引用标准第6.5.3.2节“地址和间接操作符”
一元运算
&
符的操作数应该是函数指定符,[]
一元运算*
符或一元运算符的结果,或者是指定不是位字段且未用register
存储类说明符声明的对象的左值。
OTOH,关于条件运算符的结果类型,第6.5.15章,脚注
条件表达式不产生左值。
试想一下,&5
不可能。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句