开发者

l-value query in C

开发者 https://www.devze.com 2023-03-31 18:32 出处:网络
I had c开发者_Go百科onfusion about the -l value and r-value. consider the code int x; x=5; with int x

I had c开发者_Go百科onfusion about the -l value and r-value. consider the code

int x; x=5;

with

int x

memory space is reserved for int variable. then, value 5 is assigned to it. my question is

  1. the declaration means that x is a l-value?
  2. 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?


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 is an lvalue, but &x is not - you can't re-assign the address of x to something else.

If you have a function like int* getIntPtr() then getIntPtr() isn't an lvalue (writing getIntPtr() = 5 doesn't make any sense), but *(getIntPtr()) is.

Edit: Apparently, it's not quite 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.


An l-value makes sense on the left side of an assignment. All l-values are also 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

The expression x+5 is not an l-value.


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[5] ;  // &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.

so

int x;

Now 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. Now 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 : First, 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 x=5;.

Now, the question of L-Value and R-Value :

L-Value : It is something to which a constant value can be assigned. So, yes x is a L-Value. Because we can do x=5;. But &x is not a L-Value since we can-not do &x=5;. Similarly, 0xyyyy is the address and can be a R-Value but can-not be assigned so, it is not L-Value.

0

精彩评论

暂无评论...
验证码 换一张
取 消