任何人都可以帮我解释上面代码的输出.. 它将在不同的编译器中打印不同的输出。哪个 一个要考虑的。
代码
#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;
}
输出
0,2,2
此输出来自 Codeblocks
最佳答案
你的代码等同于:
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
}
那么发生的是 b=c;
关于c - 使用条件运算符时变量值如何变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38497412/