需要左值作为赋值的左操作数
为什么我要进行
lvalue required as left operand of assignment
单个字符串比较?如何在 C
中解决此问题?
if (strcmp("hello", "hello") = 0)
谢谢!
Why am I getting
lvalue required as left operand of assignment
with a single string comparison? How can I fix this in C
?
if (strcmp("hello", "hello") = 0)
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您需要比较,而不是赋值:
因为您想检查
strcmp("hello", "hello")
的结果是否等于0
。关于错误:
lvalue
表示可赋值的值(变量),并且在赋值时=
的左值必须是lvalue
(非常清楚)。函数结果和常量都是不可赋值的(
rvalue
),因此它们是rvalue
。所以顺序并不重要,如果你忘记使用==
你会得到这个错误。 (编辑:)我认为相比之下,将常量放在左侧是一个很好的做法,因此如果您写=
而不是==
,你会得到一个编译错误。例如:与
(请参阅此问题的第一个答案:https://stackoverflow.com/questions /2349378/你创造的新编程术语)
You need to compare, not assign:
Because you want to check if the result of
strcmp("hello", "hello")
equals to0
.About the error:
lvalue
means an assignable value (variable), and in assignment the left value to the=
has to belvalue
(pretty clear).Both function results and constants are not assignable (
rvalue
s), so they arervalue
s. so the order doesn't matter and if you forget to use==
you will get this error. (edit:)I consider it a good practice in comparison to put the constant in the left side, so if you write=
instead of==
, you will get a compilation error. for example:vs.
(see first answer to this question: https://stackoverflow.com/questions/2349378/new-programming-jargon-you-coined)
您不能将右值分配给右值。
是错误的。建议:
=
是赋值运算符。==
是等于运算符。我知道许多新程序员对这个事实感到困惑。
You cannot assign an rvalue to an rvalue.
is wrong. Suggestions:
=
is the assign operator.==
is the equal to operator.I know many new programmers are confused with this fact.
将
=
更改为==
IE
if (strcmp("hello", "hello") == 0)
您想要将
strcmp()
的结果与 0 进行比较。因此您需要= = 。将其分配给 0 将不起作用,因为无法分配右值。
Change
=
to==
i.e
if (strcmp("hello", "hello") == 0)
You want to compare the result of
strcmp()
to 0. So you need==
. Assigning it to 0 won't work because rvalues cannot be assigned to.您正在尝试为函数赋值,这在 C 中是不可能的。请尝试使用比较运算符:
You are trying to assign a value to a function, which is not possible in C. Try the comparison operator instead:
我发现在处理数学时这个问题的答案是左侧的运算符必须是您要更改的变量。逻辑不能放在第一位。
这不是对您的问题的直接答案,但它可能对将来搜索我搜索过的相同内容的人有所帮助。
I found that an answer to this issue when dealing with math is that the operator on the left hand side must be the variable you are trying to change. The logic cannot come first.
This isn't a direct answer to your question but it might be helpful to future people who google the same thing I googled.
正在尝试将 0 分配给不是左值的函数返回值。
函数返回值不是左值(没有存储空间),因此任何将值赋给非左值的尝试都会导致错误。
避免 if 条件中出现此类错误的最佳实践是在比较的左侧使用常量值,因此即使使用“=”而不是“==”,常量不是左值也会立即给出错误,并避免意外赋值并导致 false if 条件为正。
Is trying to assign 0 to function return value which isn't lvalue.
Function return values are not lvalue (no storage for it), so any attempt to assign value to something that is not lvalue result in error.
Best practice to avoid such mistakes in if conditions is to use constant value on left side of comparison, so even if you use "=" instead "==", constant being not lvalue will immediately give error and avoid accidental value assignment and causing false positive if condition.