I had confusion about the -l value and r-value. consider the code
memory space is reserved for int variable. then, value 5 is assigned to it. my question is
- the declaration means that x is a l-value?
- if x has address 0xyyyy in memory, &x refers to this address.Is this address 0xyyy the l-value i.e &x is the l-value? but , the address of var is pointer, so , then, l-value becomes pointer variable?
preguntado el 27 de agosto de 11 a las 18:08
As far as I understand, an lvalue is just a fancy term for an expression that you can assign to. In other words, if something can appear on the left side of a = operator, it is an lvalue. That means
x es un valor, pero
&x is not - you can't re-assign the address of x to something else.
If you have a function like
int* getIntPtr() luego
getIntPtr() isn't an lvalue (writing
getIntPtr() = 5 doesn't make any sense), but
Edit: Apparently, it's not exactamente as easy. If x was defined as const,
x would still be an lvalue (called a non-modifiable lvalue), even though you cannot assign to it. I'm not sure if there are other exceptions.
l-value makes sense on the left side of an assignment. All
l-values son también
r-values. Whether memory is needed or not has little to do with it being an
l-value. More to the point is whether it evaluates to a memory location where something can be stored.
int x = 3; x+5 = 7; // error
x+5 no es un
lValue => If you can take address of an expression, then it is a lValue.
10 = a; // Can we take the address of 10 ?
rValue => If you can not take the address of an express, then it is rValue.
a = 10;
There are still some exceptions. For example, array type is an lValue whose address cannot be taken though or assigned to.
int a ; // &a => Not valid a = /* some thing */ // Not valid
You guys are right, but I think there may be just a little more to the OP's question. This is also something that's bugged me too.
x is an lvalue. But what is x exactly? Is it a place in memory where you can store stuff? But wait... that would be
&x, which isn't an lvalue.
Another strange thing, is that
x doesn't have to even have a location in memory. The compiler might chose to leave it in a register the whole time. AHORA what makes it an lvalue?
I think the best way to summarize is that an
lvalue is a concept the compiler uses, but that doesn't show up in the runtime.
x might be an lvalue because the compiler knows it can
store $x %ax
(I make up assembly syntax; been too long)
Or maybe it's in a register and it knows it can
move %bx %ax
Basically, it's an lvalue because the compiler knows how to store something in "it", but you can't grab a hold of that "it".
To be clear :
int x doesn't reserves the space for the variable, it just notifies the compiler about the variable. The space is reserved and assigned when the value is assigned to it, i.e when we do
Now, the question of L-Value and R-Value :
L-Value : It is something to which a constant value can be assigned.
x is a L-Value. Because we can do
&x is not a L-Value since we can-not do
0xyyyy is the address and can be a R-Value but can-not be assigned so, it is not L-Value.