An lvalue is a value that is valid on the left-hand side of an assignment statement. The name 'lvalue' means just that: left-value. I'll give a quick-and-dirty C example:

let's say you have

int x;
then you can (assuming correct scope), assign
x = 3;
because x is a regular variable that you can assign a value to. It's perfectly okay to have x on the left side of that equal sign. The compiler would fart at you if you tried something like
3 = x;
Because 3 is a numeric constant, not an lvalue. It's pretty obvious, I think. There are more subtle examples as well; as mentioned in a == writeup a pointer dereference results in an lvalue, so something like
int *x;
...
*x = 3;

Is valid for the compiler (but at run-time only if x has been properly initialized, otherwise you get a segmentation fault at best, erratic behavior at worst.)

More precisely, a value in a computer program that is guaranteed to have a named location (in memory or in a register) by the language definition. Originally the name "lvalue" came from it being something that made sense on the LHS of an assignment statement (and obviously said LHS must have some named location, or we cannot assign to it, or cannot ever find the assigned value again).

However, the distinction is (somewhat) important. C and (to a much great extent) C++ have a concept of constant lvalue! Naturally, it never makes sense to have a constant on the LHS of an assignment statement. But a statement like const double pi = 4*atan(1.0); does create an lvalue! We can say &pi to get a pointer (of type const double *).

It seems like the operative definition of an lvalue in C and C++ is "anything to which the address-of operator & may be applied". Of course, this still doesn't deal with overloading operator&() in C++; it might be better to think of an lvalue there as anything that may be passed by reference.

By contrast, you cannot take the address even of as simple an expression as 2*pi*r. So it's not an lvalue. And, of course, there's no reason for it to have a named location.

Log in or register to write something here or to contact authors.