C 中的左值查询
我对 -l 值和 r 值感到困惑。 考虑代码
int x; x=5;
与
整数x
内存空间是为int变量保留的。然后,将值5分配给它。 我的问题是
- 声明意味着 x 是左值?
- 如果x在内存中有地址0xyyyy,&x指的是这个地址。这个地址是0xyyy吗 左值 即 &x 是左值?但是 var 的地址是指针,所以 ,那么,左值 变成指针变量?
I had confusion 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
- 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
据我了解,左值只是一个可以赋值的表达式的奇特术语。换句话说,如果某个东西可以出现在 = 运算符的左侧,那么它就是左值。这意味着
x
是左值,但&x
不是 - 您不能将 x 的地址重新分配给其他内容。如果您有像
int* getIntPtr()
这样的函数,则getIntPtr()
不是左值(编写getIntPtr() = 5
则不然)没有任何意义),但是*(getIntPtr())
是。编辑:显然,这并不那么容易。如果 x 被定义为 const,
x
仍然是一个左值(称为不可修改的左值),即使您无法为其赋值。我不确定是否还有其他例外。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()
thengetIntPtr()
isn't an lvalue (writinggetIntPtr() = 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.l-value
在赋值的左侧有意义。所有左值
也是右值
。是否需要内存与其是否是左值没有什么关系。更重要的是它是否计算出可以存储某些内容的内存位置。表达式
x+5
不是l-value
。An
l-value
makes sense on the left side of an assignment. Alll-values
are alsor-values
. Whether memory is needed or not has little to do with it being anl-value
. More to the point is whether it evaluates to a memory location where something can be stored.The expression
x+5
is not anl-value
.lValue =>如果您可以获取表达式的地址,那么它就是左值。
rValue =>如果你不能获取快递的地址,那么它就是rValue。
仍有一些例外。例如,数组类型是一个lValue,其地址不能被获取或赋值。
lValue => If you can take address of an expression, then it is a lValue.
rValue => If you can not take the address of an express, then it is rValue.
There are still some exceptions. For example, array type is an lValue whose address cannot be taken though or assigned to.
你们是对的,但我认为OP的问题可能还有更多内容。这也是让我很烦恼的事情。
所以
现在 x 是一个左值。但x到底是什么?它是内存中可以存储东西的地方吗?但是等等...这将是
&x
,它不是左值。另一个奇怪的事情是,
x
甚至不必在内存中拥有一个位置。编译器可能会选择始终将其保留在寄存器中。 现在是什么使它成为左值?我认为最好的总结方式是,左值是编译器使用的一个概念,但它不会出现在运行时中。
x
可能是一个左值,因为编译器知道它可以(我编写了汇编语法;太长了)
或者它可能在寄存器中并且它知道它可以
基本上,它是一个左值,因为编译器知道如何将某些东西存储在“它”中,但你无法抓住那个“它”。
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
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(I make up assembly syntax; been too long)
Or maybe it's in a register and it knows it can
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".
需要明确的是:
首先,
int x
不为变量保留空间,它只是通知编译器有关该变量的信息。当给它赋值时,即当我们执行x=5;
时,该空间被保留并分配。现在,L-值和R-值的问题:
L-值:它是可以分配恒定值的东西。
所以,是的,
x
是一个 L 值。因为我们可以做到x=5;
。但是
&x
不是 L 值,因为我们不能这样做&x=5;
。同样,0xyyyy
是地址,可以是 R 值,但不能分配,因此它不是 L 值。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 dox=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 dox=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.