consulta de valor l en C

I had confusion about the -l value and r-value. consider the code

int x; x=5;


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?

preguntado el 27 de agosto de 11 a las 18:08

5 Respuestas

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 *(getIntPtr()) .

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.

Respondido 27 ago 11, 23:08

A closer approximation is that an lvalue is an expression that designates an object. The flaw in que definition is that, given int *ptr = NULL;, *ptr is still an lvalue. - Keith Thompson

An array is also an lvalue but you cannot assign to it. - R .. GitHub DEJA DE AYUDAR A ICE

An 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

La expresion x+5 no es un l-value.

Respondido 27 ago 11, 23:08

The C standard doesn't use the term "rvalue" except in one footnote, where it says that it's merely "the value of an expression" (whereas an lvalue is the expression itself, if the expression designates an object). I personally find this a bit confusion, and inconsistent with the original meanings of the terms, but when discussing C it's best to use the term "lvalue" as the standard defines it. - Keith Thompson

@Keith Thompson: I'm pretty sure K&R mention lvalue y rvalue, Aunque rvalue is much less used. - Wallyk

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

Respondido 27 ago 11, 23:08

&a not valid!? Isn't it the same than a? - calandoa

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.


int x;

AHORA 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".

Respondido 27 ago 11, 23:08

this is not an answer, you're probably floating in the same lack of confidence as the op. - unkulunkulu

Es la pregunta verdaderamente just "what is an lvalue?" If it were I think they would have found an answer by now. - Owen

What is x exactly? It's a variable! In C, it is (conceptually) the name of a memory location where you can store values, with associated information about type and constness and maybe other things. &x is the dirección of that memory location. You can't store your information in the location's address. You store it in the location. Of course the compiler can optimize the code so the variable is only stored in a register, as long as the result behaves the same. But that doesn't change what the variable is conceptually. - Medo42

Just to be clear, I meant "what is x exactly" rhetorically. If you've ever tried designing a language, this is not a trivial question. - Owen

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;. Pero &x is not a L-Value since we can-not do &x=5;. Similar, 0xyyyy is the address and can be a R-Value but can-not be assigned so, it is not L-Value.

Respondido 27 ago 11, 23:08

"int x doesn't reserves the space for the variable" Yes it does. Compile file t.c that contains only "int x;" and launch nm t.o. - Pascal Cuoq

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.