Can anyone tell me in simple words that what this L value is and why I'm coming across the error "L Value required in function main()"?
-
6Please post the code you're trying to compile. – Greg Hewgill Apr 10 '10 at 20:42
-
1You are probably trying to cast the left operand of an assignment operation. – Alex Apr 10 '10 at 20:43
-
L means left. Left side of the assignment operator. See http://en.wikipedia.org/wiki/Value_(computer_science) – JRL Apr 10 '10 at 20:46
-
http://stackoverflow.com/questions/579421/often-used-seldom-defined-terms-lvalue – Apr 10 '10 at 20:59
2 Answers
Lvalue is something which can be assigned to, or taken a pointer of.
It's something that has an address.
Example:
int f()
{
return 5;
}
f() = 8; // doesn't compile, f() is not an lvalue
int* g()
{
int* pv = malloc(sizeof(int));
*pv = 5;
return pv;
}
*g() = 8; // compiles, as *g() is a lvalue
If you post your code, we will be able to tell you why are you getting the error message about the missing lvalue.

- 35,022
- 6
- 77
- 199
-
(A nitpick) Not exactly correct. For example, a constant bit-field is an lvalue. Yet, you can't assign to it and you can't take its address :) Also, you can take address of a function, but in C functions are not lvalues. – AnT stands with Russia Apr 10 '10 at 21:03
-
@AndreyT: Okay, if going into details, a pointer to constant variable is an lvalue, but not assignable. I tried to give more a feeling of lvalue than the formal definition covering all the cases. – Vlad Apr 10 '10 at 21:10
-
An array is also a non-modifiable lvalue (you can take its address, but not assign to it). – caf Apr 11 '10 at 00:45
An lvalue is a term given to an expression that refers to an object, i.e. something with an address.
Historically it comes from the the fact that it's something that's valid to appear on the left of an assignment. In contrast, something that can appear on the right of an assignment is known was an rvalue, however rvalue actually refers to any expression that isn't an lvalue.
Typically you can convert lvalues to rvalues (objects have a value), but not the other way around.
Usually the error that you are getting means that you are trying to do something to an rvalue that is only valid for lvalues.
That might be, assigning to the result of a function, or taking the address of a literal.
f() = 5;
int *p = &5;

- 755,051
- 104
- 632
- 656