What is an lvalue? (Perl, C)
Archive - Originally posted on "The Horse's Mouth" - 2008-03-18 18:48:27 - Graham EllisAn lvalue is an expression that you can write on the left hand side of an assignment statement - in other words an expression that defines a specific memory address of a variable.
The most common lvalues are simple variables or array / list / hash / dictionary members ... for example
$hello = "Hello World";
$greet[5] = "Hello World";
$greet{"UK"} = "Hello World"; # all these in Perl
val = 15;
jolly[posn] = 27; # These two in C
There are some surprises until you think about it.
In Perl you can write $lhs[$n+4] = 17; but not $lhs+4 = 17;. Yet in C, if vvv is a pointer to an array you can write vvv+4 = 15; quite happily - altering the fifth element of the array.
You'll find the term lvalue come up most usually for the programmer in a compiler error message - "invalid lvalue" you will be told. What the compiler really means is that the twit who wrote the code is trying to save a value into an expression that doesn't actually define the memory address of a variable.