简体   繁体   English

(赋值中无效的左值)我运行它时会发生此错误。这是什么意思?

[英](invalid lvalue in assignment) this error happens when i run it.what does it mean?

this is the code and the compiler says there is sth wrong with line 7. 这是代码,编译器说第7行存在某些错误。

include<stdio.h>
main()
{
char m;
 int a,b,n=0;
scanf("%c%d%d",&m,&a,&b);
m=='A' || m=='B' || m=='C' ? n=(3*a)+(5*b) : n=(5*a)+(3*b);
printf("%d\n",n);
}

Use instead 改用

m=='A' || m=='B' || m=='C' ? n=(3*a)+(5*b) : ( n=(5*a)+(3*b));

Otherwise the statement looks like 否则,语句看起来像

( m=='A' || m=='B' || m=='C' ? n=(3*a)+(5*b) : n)=(5*a)+(3*b);

Or you could write 或者你可以写

n = m=='A' || m=='B' || m=='C' ? (3*a)+(5*b) : (5*a)+(3*b);

The conditional operator in C is defined the following way C中的条件运算符通过以下方式定义

conditional-expression:
    logical-OR-expression
    logical-OR-expression ? expression : conditional-expression

As the assignment operator has lower priority then the compiler issues an error because the assignment is excluded from the conditional operator for the third operand 由于赋值运算符的优先级较低,因此编译器将发出错误,因为该赋值已从第三个操作数的条件运算符中排除

The used by you expression would be valid in C++ because in C++ the operator is defined differently 您使用的表达式在C ++中将是有效的,因为在C ++中,运算符的定义不同

conditional-expression:
    logical-or-expression
    logical-or-expression ? expression : assignment-expression
                                         ^^^^^^^^^^^^^^^^^^^^^

There is no need to use a complicated statement that confuses everyone, including the compiler. 无需使用会使所有人(包括编译器)困惑的复杂语句。 This is just as effective, and a lot easier to read: 这同样有效,而且更容易阅读:

if (m=='A' || m=='B' || m=='C')
   n=(3*a)+(5*b);
else
   n=(5*a)+(3*b);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 需要左值作为赋值的左操作数。 这意味着什么? - lvalue required as left operand of assignment. What does that mean? 错误:赋值中的左值无效[in c] - Error: invalid lvalue in assignment [in c] 尝试使指针为NULL时,赋值错误中的左值无效 - Invalid lvalue in assignment error when trying to make a pointer NULL 我完成这项作业后会发生什么 - what exactly happens when I do this assignment “错误:对具有数组类型的表达式的赋值”是什么意思? - What does “error: assignment to expression with array type” mean? 左值需要作为赋值的左操作数 - 是什么导致了这个错误以及如何修复它? - lvalue required as left operand of assignment - What causes this error and how to fix it? 我如何调试错误:左值作为赋值的左操作数需要左值? - how do i debug error: lvalue required as left operand of assignment? 当我给strftime无效的说明符时会发生什么? - What happens when I give strftime invalid specifier? 使用malloc时的编译器错误(左值要求为赋值的左操作数) - Compiler error (lvalue required as left operand of assignment) when using malloc 为什么指针分配在分配看起来合适时显示左值错误? - Why Pointer assignment shows lvalue error when assignments look appropriate?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM