Friday, May 16, 2008

What is an lvalue?

example to demonstrate lvalue

#define CONSTANT_VAR 10

int var=1;

int* pntr_int=5;

CONSTANT_VAR = 15;

The variable var is an integer, which is a storable location in memory. Therefore, the statement var = 1 qualifies var to be an lvalue. Notice the second assignment statement, *pntr_int = 5. By using the * modifier to reference the area of memory that pntr_int points to, *pntr_int is qualified as an lvalue.

In contrast, observe the CONSTANT variable:

In this 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, this assignment statements do not contain lvalues and will be flagged by your compiler as errors.

1 comment:

shiva said...

CONSTANT_VAR IS NOT a constant just that to intialize globally

Contributors