Monday, May 16, 2011

What is an lvalue?


An lvalue is an expression to which a value can be assigned. The lvalue expression is located on the left side
of an assignment statement, whereas an rvalue (see FAQ I.11) is located on the right side of an assignment
statement. Each assignment statement must have an lvalue and an rvalue. The lvalue expression must
reference a storable variable in memory. It cannot be a constant. For instance, the following lines show a few
examples of lvalues:
int x;
int* p_int;
x = 1;
*p_int = 5;
The variable x is an integer, which is a storable location in memory. Therefore, the statement x = 1 qualifies
x to be an lvalue. Notice the second assignment statement, *p_int = 5. By using the * modifier to reference
the area of memory that p_int points to, *p_int is qualified as an lvalue. In contrast, here are a few examples
of what would not be considered lvalues:
#define CONST_VAL 10
int x;
/* example 1 */
1 = x;
/* example 2 */
CONST_VAL = 5;
In both statements, the left side of the statement evaluates to a constant value that cannot be changed because
constants do not represent storable locations in memory. Therefore, these two assignment statements do not
contain lvalues and will be flagged by your compiler as errors.