tags:

views:

110

answers:

5
+1  Q: 

lvalue required

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

+3  A: 

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

Paul Dixon
+3  A: 

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 register or to a memory location. Something like 42 is a value that matches neither criteria. More formally there are three categories

  • Lvalues: Referring to objects. This includes objects declared const. Such are non-modifiable lvalues.
  • Function designators: Referring to functions. printf is a function designator, but &printf is not, while *&printf is 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. &printf belongs to this category.
Johannes Schaub - litb
A: 

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.

Peter Miehle
+1  A: 

The C99 standard states (6.3.2.1):


An lvalue is an expression with an object type or an incomplete type other than void; if an lvalue does 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 lvalue used to designate the object. A modifiable lvalue is an lvalue that 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.

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


In other words, an lvalue is something that you can locate for potentially changing. A modifiable lvalue is one that you're actually allowed to change.

For example, the C statement:

x = 7;

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

14 = 7;

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

The snippet:

const int x = 7;

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

paxdiablo
@paxdiablo: nit picking, but technically this can also be a variable that is declared with `register`, so it might not have an address.
Jens Gustedt
A: 

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;

Sid_M