C语言 左值需要作为赋值的左操作数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6162438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:47:23  来源:igfitidea点击:

lvalue required as left operand of assignment

clvaluestrcmp

提问by Joseph

Why am I getting

为什么我得到

lvalue required as left operand of assignment

with a single string comparison? How can I fix this in C?

与单个字符串比较?我该如何解决这个问题C

if (strcmp("hello", "hello") = 0)

Thanks!

谢谢!

回答by MByD

You need to compare, not assign:

您需要比较,而不是分配:

if (strcmp("hello", "hello") == 0)
                             ^

Because you want to check if the result of strcmp("hello", "hello")equals to 0.

因为您想检查结果是否strcmp("hello", "hello")等于0.

About the error:

关于错误:

lvalue required as left operand of assignment

左值需要作为赋值的左操作数

lvaluemeans an assignable value (variable), and in assignment the left value to the =has to be lvalue(pretty clear).

lvalue意味着一个可赋值的值(变量),并且在赋值时=必须是lvalue(很清楚)的左值。

Both function results and constants are not assignable (rvalues), so they are rvalues. so the order doesn't matter and if you forget to use ==you will get this error. (edit:)I consider ita 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:

函数结果和常量都是不可赋值的(rvalues),所以它们都是rvalues。所以顺序无关紧要,如果您忘记使用,==您将收到此错误。(编辑:)与将常量放在左侧相比,我认为这是一个很好的做法,所以如果你写=而不是==,你会得到一个编译错误。例如:

int a = 5;
if (a = 0) // Always evaluated as false, no error.
{
    //...
}

vs.

对比

int a = 5;
if (0 = a) // Generates compilation error, you cannot assign a to 0 (rvalue)
{
    //...
}

(see first answer to this question: https://stackoverflow.com/questions/2349378/new-programming-jargon-you-coined)

(见这个问题的第一个答案:https: //stackoverflow.com/questions/2349378/new-programming-jargon-you-coined

回答by EKons

You cannot assign an rvalue to an rvalue.

您不能将右值分配给右值。

if (strcmp("hello", "hello") = 0)

is wrong. Suggestions:

是错的。建议:

if (strcmp("hello", "hello") == 0)
                              ^

=is the assignoperator.
==is the equal tooperator.
I know many new programmers are confused with this fact.

=赋值运算符。
==等于运算符。
我知道许多新程序员对这个事实感到困惑。

回答by Prasoon Saurav

Change =to ==i.e if (strcmp("hello", "hello") == 0)

更改===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.

您想将 的结果strcmp()与 0进行比较==。因此您需要. 将其分配给 0 将不起作用,因为无法分配右值。

回答by Prasoon Saurav

You are trying to assign a value to a function, which is not possible in C. Try the comparison operator instead:

您正在尝试为函数赋值,这在 C 中是不可能的。请尝试使用比较运算符:

if (strcmp("hello", "hello") == 0)

回答by Thomas

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.

我发现在处理数学时这个问题的答案是左侧的运算符必须是您要更改的变量。逻辑不能放在首位。

coin1 + coin2 + coin3 = coinTotal; // Wrong

coinTotal = coin1 + coin2 + coin3; // Right

This isn't a direct answer to your question but it might be helpful to future people who google the same thing I googled.

这不是对您问题的直接回答,但它可能对未来使用我在 google 上搜索的相同内容的人有所帮助。

回答by Parshuram Thombare

if (strcmp("hello", "hello") = 0)

Is trying to assign 0 to function return value which isn't lvalue.

试图将 0 分配给不是左值的函数返回值。

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.

在 if 条件下避免此类错误的最佳实践是在比较的左侧使用常量值,因此即使使用“=”代替“==”,常量不是左值也会立即出错并避免意外赋值并导致错误如果条件为正。