Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lvalue required

Tags:

c

lvalue

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

like image 266
alfesani Avatar asked Oct 09 '10 08:10

alfesani


3 Answers

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

  • often used seldom defined terms: lvalue
  • lvalue and rvalue
  • “l-value required” error
like image 153
Paul Dixon Avatar answered Nov 05 '22 04:11

Paul Dixon


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.
like image 22
Johannes Schaub - litb Avatar answered Nov 05 '22 02:11

Johannes Schaub - litb


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).

like image 39
paxdiablo Avatar answered Nov 05 '22 03:11

paxdiablo