使用条件运算符时变量值如何变化?

Cre*_*cky 0 c pointers ternary-operator conditional-operator

任何人都可以帮我解释上面代码的输出..它将在不同的编译器中打印不同的输出.哪一个要考虑.

#include<stdio.h>

int main()
{
    int a=0, b=1, c=2;
    *((a+1 == 1) ? &b : &a) = a ? b : c;
    printf("%d, %d, %d\n", a, b, c);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

产量

0,2,2
Run Code Online (Sandbox Code Playgroud)

此输出来自Codeblocks

438*_*427 6

您的代码相当于:

int* ptr;

if (a + 1 == 1)   // which is true
{
    ptr = &b;     // So ptr points to b
}
else
{
    ptr = &a;
}

if (a != 0)       // which is false
{
    *ptr = b;    
}
else
{
    *ptr = c;      // so *ptr (which is same as b) is set equal to c
}
Run Code Online (Sandbox Code Playgroud)

那么会发生什么 b=c;