user-0 提问: 我这样的写法有什么不对吗？

回复:
user: user-1
content: # include <stdio.h>  int main() {     int mb = 0;     int a = 0, b = 0;      mb = 0;     a = 0;     b = 0;     printf("a = %d, b = %d\n", a, b);     mb == 0 ? (a = 1) : (b = 1);     printf("a = %d, b = %d\n", a, b);      mb = 1;     a = 0;     b = 0;     printf("a = %d, b = %d\n", a, b);     mb == 0 ? (a = 1) : (b = 1);     printf("a = %d, b = %d\n", a, b);      mb = 0;     a = 0;     b = 0;     printf("a = %d, b = %d\n", a, b);     *(mb == 0 ? &a : &b) = 1;     printf("a = %d, b = %d\n", a, b);      return 0;

user: user-2
content: 不管只知不知道优先级，多加个括号总是不会错的，这也是一种好的编程习惯

user: user-3
content: y = (x != y ? s1 : s2).i;

user: user-4
content: 引用 12 楼 mbctb 的回复:[quote=引用 11 楼 woshiyuanlei 的回复:] 楼上不是都已经有人回答了是右值么。。右值不能赋值。。像2楼那样就可以赋值了。。自己百度一下左值右值吧 是的，我看到楼上的了，但是我有个疑问，既然它是个右值，不能赋值，那编译器应该报错啊？可是编译器没有报错。这个问题我是经过很艰苦的调试才找到的。  我想知道编译器把它误会成什么了？为什么会有这样的误会？[/quote]  怪我刚才妄下评论，这是汇编，结果很明显  	int csdn_c=0,csdn_a=0,csdn_b=0; 00412D90  mov         dword ptr [ebp-18h],0    //csdn_c=0 00412D97  mov         dword ptr [ebp-24h],0    //csdn_a=0 00412D9E  mov         dword ptr [ebp-30h],0   //csdn_b=0 	csdn_c==0?csdn_a:csdn_b=1; 00412DA5  cmp         dword ptr [ebp-18h],0    //csdn_c==0 00412DA9  jne         wmain+66h (412DB6h)  //假如csdn_c不等于0，就跳到00412DB6处 00412DAB  mov         eax,dword ptr [ebp-24h]   //这句和后面两句没太多用,总之csdn_a的值未变 00412DAE  mov         dword ptr [ebp-5C0h],eax   00412DB4  jmp         wmain+76h (412DC6h)   00412DB6  mov         dword ptr [ebp-30h],1      //假如csdn_c不等于0，就跳到这里,这时给csdn_b赋值1 00412DBD  mov         ecx,dword ptr [ebp-30h]   00412DC0  mov         dword ptr [ebp-5C0h],ecx   	cout<<"csdn_a="<<csdn_a<<" csdn_b="<<csdn_b<<endl; 00412DC6  mov         esi,esp   00412DC8  mov         eax,dword ptr [__imp_std::endl (43055Ch)]      依据我的分析，你的代码中如果mb==0为假，那么b是可以赋值成功的，a则不行。。

user: user-5
content: 二楼正解！！

user: user-6
content: 不要迷信书、考题、老师、回帖； 要迷信CPU、编译器、调试器、运行结果。 并请结合“盲人摸太阳”和“驾船出海时一定只带一个指南针。”加以理解。 任何理论、权威、传说、真理、标准、解释、想象、知识……都比不上摆在眼前的事实！  有人说一套做一套，你相信他说的还是相信他做的？ 其实严格来说这个世界上古往今来所有人都是说一套做一套，不是吗？  不要写连自己也预测不了结果的代码！

user: user-7
content: 一楼正解。 你可以在编译器中查看错误原因。

user: user-8
content: ?和= 结合性是从右到左

user: user-9
content: mb == 0 ? (a=1) : (b = 1); //你的代码相当于 if(mb == 0)     a ; else     b = 1;

user: user-0
content: 引用 11 楼 woshiyuanlei 的回复:楼上不是都已经有人回答了是右值么。。右值不能赋值。。像2楼那样就可以赋值了。。自己百度一下左值右值吧 是的，我看到楼上的了，但是我有个疑问，既然它是个右值，不能赋值，那编译器应该报错啊？可是编译器没有报错。这个问题我是经过很艰苦的调试才找到的。  我想知道编译器把它误会成什么了？为什么会有这样的误会？

user: user-4
content: 楼上不是都已经有人回答了是右值么。。右值不能赋值。。像2楼那样就可以赋值了。。自己百度一下左值右值吧

user: user-0
content: 引用 8 楼 LouisScola 的回复:搞不清楚优先级的时候就加（） 可是，？　：　的　优先级不是比　＝　高吗？ 所以我觉得：mb == 0 ? a : b = 1; 就等于：(mb == 0 ? a : b) = 1; 这有什么不对吗？

user: user-6
content: 自己写代码搞不清楚算符优先级请多加括号。 看别人代码搞不清楚算符优先级能调试的话请单步调试对应汇编。 看别人代码搞不清楚算符优先级不能调试的话想办法照写一小段孤立的可调试的代码然后单步调试对应汇编。 看别人代码搞不清楚算符优先级不能调试的话且没有办法照写一小段孤立的可调试的代码然后单步调试对应汇编的话只能参考算符优先级表猜了（提醒：并不能100%猜对）。 //C++ Operators //  Operators specify an evaluation to be performed on one of the following: //    One operand (unary operator) //    Two operands (binary operator) //    Three operands (ternary operator) //  The C++ language includes all C operators and adds several new operators. //  Table 1.1 lists the operators available in Microsoft C++. //  Operators follow a strict precedence which defines the evaluation order of //expressions containing these operators.  Operators associate with either the //expression on their left or the expression on their right;    this is called //“associativity.” Operators in the same group have equal precedence and are //evaluated left to right in an expression unless explicitly forced by a pair of //parentheses, ( ). //  Table 1.1 shows the precedence and associativity of C++ operators //  (from highest to lowest precedence). // //Table 1.1   C++ Operator Precedence and Associativity // The highest precedence level is at the top of the table. //+------------------+-----------------------------------------+---------------+ //| Operator         | Name or Meaning                         | Associativity | //+------------------+-----------------------------------------+---------------+ //| ::               | Scope resolution                        | None          | //| ::               | Global                                  | None          | //| [ ]              | Array subscript                         | Left to right | //| ( )              | Function call                           | Left to right | //| ( )              | Conversion                              | None          | //| .                | Member selection (object)               | Left to right | //| ->               | Member selection (pointer)              | Left to right | //| ++               | Postfix increment                       | None          | //| --               | Postfix decrement                       | None          | //| new              | Allocate object                         | None          | //| delete           | Deallocate object                       | None          | //| delete[ ]        | Deallocate object                       | None          | //| ++               | Prefix increment                        | None          | //| --               | Prefix decrement                        | None          | //| *                | Dereference                             | None          | //| &                | Address-of                              | None          | //| +                | Unary plus                              | None          | //| -                | Arithmetic negation (unary)             | None          | //| !                | Logical NOT                             | None          | //| ~                | Bitwise complement                      | None          | //| sizeof           | Size of object                          | None          | //| sizeof ( )       | Size of type                            | None          | //| typeid( )        | type name                               | None          | //| (type)           | Type cast (conversion)                  | Right to left | //| const_cast       | Type cast (conversion)                  | None          | //| dynamic_cast     | Type cast (conversion)                  | None          | //| reinterpret_cast | Type cast (conversion)                  | None          | //| static_cast      | Type cast (conversion)                  | None          | //| .*               | Apply pointer to class member (objects) | Left to right | //| ->*              | Dereference pointer to class member     | Left to right | //| *                | Multiplication                          | Left to right | //| /                | Division                                | Left to right | //| %                | Remainder (modulus)                     | Left to right | //| +                | Addition                                | Left to right | //| -                | Subtraction                             | Left to right | //| <<               | Left shift                              | Left to right | //| >>               | Right shift                             | Left to right | //| <                | Less than                               | Left to right | //| >                | Greater than                            | Left to right | //| <=               | Less than or equal to                   | Left to right | //| >=               | Greater than or equal to                | Left to right | //| ==               | Equality                                | Left to right | //| !=               | Inequality                              | Left to right | //| &                | Bitwise AND                             | Left to right | //| ^                | Bitwise exclusive OR                    | Left to right | //| |                | Bitwise OR                              | Left to right | //| &&               | Logical AND                             | Left to right | //| ||               | Logical OR                              | Left to right | //| e1?e2:e3         | Conditional                             | Right to left | //| =                | Assignment                              | Right to left | //| *=               | Multiplication assignment               | Right to left | //| /=               | Division assignment                     | Right to left | //| %=               | Modulus assignment                      | Right to left | //| +=               | Addition assignment                     | Right to left | //| -=               | Subtraction assignment                  | Right to left | //| <<=              | Left-shift assignment                   | Right to left | //| >>=              | Right-shift assignment                  | Right to left | //| &=               | Bitwise AND assignment                  | Right to left | //| |=               | Bitwise inclusive OR assignment         | Right to left | //| ^=               | Bitwise exclusive OR assignment         | Right to left | //| ,                | Comma                                   | Left to right | //+------------------+-----------------------------------------+---------------+

user: user-10
content: 搞不清楚优先级的时候就加（）

user: user-11
content: mb == 0 ? a : b的结果是右值，也就a或b的值，而不是其变量本身，所以不可被赋值。把代码改成*(mb == 0 ?& a:&b) = 1;即可。