C语言 需要左值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3896637/
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 06:38:24  来源:igfitidea点击:

lvalue required

clvalue

提问by alfesani

what does the error message "Lvalue required" actually mean?

错误消息“需要左值”实际上是什么意思?

回答by Paul Dixon

An lvalue is something that can appear on the left side of an assignment, in other words 'something that can be assigned'

左值是可以出现在赋值左侧的东西,换句话说就是“可以赋值的东西”

So, look for an assignment where the left hand side isn't 'assignable', for example, something as simple as this might trigger such an error

因此,寻找左侧不可“分配”的分配,例如,像这样简单的事情可能会触发此类错误

if (0 = foo)
{

}

Here you've got an attempt to assign to a constant because of accidentally using = rather than ==

在这里,您尝试分配给一个常量,因为不小心使用了 = 而不是 ==

See also

也可以看看

回答by Johannes Schaub - litb

It means the implementation expects an object, but you just passed a value or function. This happens for assignments you passed a non-lvalue or for address-of operations applied to non-functions.

这意味着实现需要一个对象,但您只是传递了一个值或函数。对于传递非左值的赋值或应用于非函数的操作地址,会发生这种情况。

Lvalue stands for "location value" and means an expression that refers to an object either declared as registeror to a memory location. Something like 42is a value that matches neither criteria. More formally there are three categories

Lvalue 代表“位置值”,意思是引用声明为register内存位置或声明为内存位置的对象的表达式。类似的42是一个不符合任何条件的值。更正式地分为三类

  • Lvalues: Referring to objects. This includes objects declared const. Such are non-modifiable lvalues.
  • Function designators: Referring to functions. printfis a function designator, but &printfis not, while *&printfis again.
  • Others: Sometimes called "rvalue" and by the Standard described as "the value of an expression". Examples are var + 0(yielding a value not associated with objects anymore), or an enumerator of an enumeration. &printfbelongs to this category.
  • 左值:引用对象。这包括声明为 const 的对象。这些是不可修改的左值。
  • 功能指示符:指功能。printf是函数指示符,但&printf不是,而又*&printf是。
  • 其他:有时称为“右值”,被标准描述为“表达式的值”。示例是var + 0(产生不再与对象关联的值)或枚举的枚举器。&printf属于这一类。

回答by paxdiablo

The C99 standard states (6.3.2.1):

C99 标准规定 (6.3.2.1):



An lvalueis an expression with an object type or an incomplete type other than void; if an lvaluedoes not designate an object when it is evaluated, the behavior is undefined. When an object is said to have a particular type, the type is specified by the lvalueused to designate the object. A modifiable lvalueis an lvaluethat does not have array type, does not have an incomplete type, does not have a const-qualified type, and if it is a structure or union, does not have any member (including, recursively, any member or element of all contained aggregates or unions) with a const-qualified type.

Anlvalue是具有对象类型或除 void 以外的不完整类型的表达式;如果 anlvalue在计算时未指定对象,则行为未定义。当一个对象被称为具有特定类型时,该类型由lvalue用于指定对象的 指定。可修改lvaluelvalue没有数组类型,没有不完整类型,没有 const 限定类型,并且如果它是结构或联合,没有任何成员(递归地包括任何成员或元素)所有包含的聚合或联合)具有 const 限定类型。

The name lvaluecomes originally from the assignment expression E1 = E2, in which the left operand E1is required to be a (modifiable) lvalue. It is perhaps better considered as representing an object "locator value". What is sometimes called rvalueis in this International Standard described as the "value of an expression".

该名称lvalue最初来自赋值表达式E1 = E2,其中要求左操作数E1为 a (modifiable) lvalue。最好将其视为表示对象“定位器值”。rvalue在本国际标准中有时称为“表达式的值”。



In other words, an lvalueis something that you can locate for potentially changing. A modifiable lvalueis one that you're actually allowedto change.

换句话说, anlvalue是您可以定位以进行潜在更改的东西。可修改lvalue是您实际上可以更改的。

For example, the C statement:

例如,C 语句:

x = 7;

is valid because xis an lvalue. On the other hand, the statement:

是有效的,因为xlvalue。另一方面,声明:

14 = 7;

is not valid because 14is notsomething you can locate for an assignment.

是无效的,因为14没有的东西,你可以找到一个分配。

The snippet:

片段:

const int x = 7;

actually creates an lvaluecalled xeven though you're not permitted to change it (it's not a modifiable `lvalue).

即使您不允许更改它,实际上也会创建一个lvalue调用x(它不是可修改的“左值”)。

回答by Sid_M

Most likely it means that you tried to assign a value to something that cannot be assigned to. For example, both of the following would probably cause that error:

很可能这意味着您试图为无法分配的值分配一个值。例如,以下两种情况都可能导致该错误:

5 = 5; myObject->myMethod() = 5;

5 = 5; myObject->myMethod() = 5;

回答by Peter Miehle

the error cometh if you code somrthing like function(parameter) = value;because you cannot assign value to anything that is not a possible container for it.

如果您编写类似的代码,就会出现错误,function(parameter) = value;因为您无法为任何不是它的可能容器的东西赋值。