左值要求作为分配的左操作数(C程序) [英] lvalue required as left operand of assignment (C program)
问题描述
我得到的误差要求作为转让的左操作数左值
。
其分配到该行:
if(ch=='.' || ch=='"' || ch=='(' || ch=')' || ch=='!' || ch==',' || ch=='?' || ch==';') { etc }
我只是想检查,如果我看的字符等于这些字符中的任何一个。在修复任何想法?
I just want to check if the character I'm looking at is equal to any one of those characters. Any idea on the fix?
感谢您,
推荐答案
CH =')
应 CH ==')
。您获得的特定错误的原因是因为在 ==
具有较高的precedence比 ||
, =
具有较低的precedence,所以这个:
ch=')'
should be ch==')'
. The reason you are getting that particular error is because while ==
has higher precedence than ||
, =
has lower precedence, so this:
ch == '(' || ch = ')'
被解析为
((ch == '(') || ch) = ')'
这是试图以分配')
到的结果(CH =='(')|| CH)
这是一个右值,而不是转让。这是一个常见的错误,并且使用这种技术来避免它:
Which is trying to assign ')'
to the result of (ch == '(') || ch)
which is an rvalue and not assignable. This is a common mistake, and this technique is used to avoid it:
if ('.' == ch || '"' == ch || ...)
注意,字符文字和变量皆已周围的 ==
的地方。这样一来,如果你不小心键入
Notice that the character literals and the variable have switched places around the ==
. That way, if you accidentally type
if ('.' = ch)
您会得到一个编译器错误。
You'll get a compiler error.
这篇关于左值要求作为分配的左操作数(C程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!