需要左值作为条件运算符中赋值的左操作数 [英] lvalue required as left operand of assignment in conditional operator
本文介绍了需要左值作为条件运算符中赋值的左操作数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include <stdio.h>
int main()
{
int a = 1, b;
a ? b = 3 : b = 4;
printf("%d, %d", a, b);
return 0;
}
[user@localhost programs]$ gcc -Wall vol.c
vol.c: In function ‘main’:
vol.c:5:16: error: lvalue required as left operand of assignment
a ? b = 3 : b = 4;
^
我给定的左值是 b
那么为什么 gcc
显示错误以及如何解决?
I have given lvalue as b
then why gcc
is showing error and how to fix it?
推荐答案
它与操作员排序有关。编译器认为您正在做什么
It has to do with operator sequencing. What the compiler thinks you're doing is
(a ? b = 3 : b) = 4
这显然是不正确的。
相反,为什么不把 b
在左侧,并仅使用条件表达式获取要分配的值,例如
Instead, why not put the b
on the left side, and get only the value to assign using the conditional expression, like
b = a ? 3 : 4;
这篇关于需要左值作为条件运算符中赋值的左操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文