« Congratulations, Martin and Marta | Main | Ruby, C, Java and more - getting out of loops »

March 18, 2008

What is an lvalue? (Perl, C)

An 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.

Posted by gje at March 18, 2008 06:48 PM

Comments

Post a comment

! Comment registration is required but no TypeKey token has been given in weblog configuration!
Well House Consultants Ltd. Copyright 2008