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

发布于 2024-11-10 09:31:16 字数 201 浏览 5 评论 0原文

为什么我要进行

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一世旳自豪 2024-11-17 09:31:16

您需要比较,而不是赋值:

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

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

关于错误:

需要左值作为左操作数
作业

lvalue表示可赋值的值(变量),并且在赋值时=的左值必须是lvalue(非常清楚)。

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

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

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

(请参阅此问题的第一个答案:https://stackoverflow.com/questions /2349378/你创造的新编程术语

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.

About the error:

lvalue required as left operand of
assignment

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

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 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:

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)

画中仙 2024-11-17 09:31:16

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

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

是错误的。建议:

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

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

You cannot assign an rvalue to an rvalue.

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

is wrong. Suggestions:

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

= is the assign operator.
== is the equal to operator.
I know many new programmers are confused with this fact.

旧伤还要旧人安 2024-11-17 09:31:16

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

苏别ゝ 2024-11-17 09:31:16

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

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

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

if (strcmp("hello", "hello") == 0)
北方的巷 2024-11-17 09:31:16

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

coin1 + coin2 + coin3 = coinTotal; // Wrong

coinTotal = coin1 + coin2 + coin3; // Right

这不是对您的问题的直接答案,但它可能对将来搜索我搜索过的相同内容的人有所帮助。

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.

挽手叙旧 2024-11-17 09:31:16
if (strcmp("hello", "hello") = 0)

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

函数返回值不是左值(没有存储空间),因此任何将值赋给非左值的尝试都会导致错误。

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文