what does the error message "Lvalue required" actually mean?
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
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
printf
is a function designator, but &printf
is not, while *&printf
is again.var + 0
(yielding a value not associated with objects anymore), or an enumerator of an enumeration. &printf
belongs to this category. 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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With