C 中的左值查询

发布于 2024-12-01 15:09:05 字数 291 浏览 1 评论 0原文

我对 -l 值和 r 值感到困惑。 考虑代码

int x; x=5;

整数x

内存空间是为int变量保留的。然后,将值5分配给它。 我的问题是

  1. 声明意味着 x 是左值?
  2. 如果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

  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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

萌无敌 2024-12-08 15:09:05

据我了解,左值只是一个可以赋值的表达式的奇特术语。换句话说,如果某个东西可以出现在 = 运算符的左侧,那么它就是左值。这意味着 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() 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.

酒浓于脸红 2024-12-08 15:09:05

l-value 在赋值的左侧有意义。所有左值也是右值。是否需要内存与其是否是左值没有什么关系。更重要的是它是否计算出可以存储某些内容的内存位置。

int  x = 3;

x+5 = 7;    // error

表达式x+5 不是l-value

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.

做个ˇ局外人 2024-12-08 15:09:05

lValue =>如果您可以获取表达式的地址,那么它就是左值。

10 = a; // Can we take the address of 10 ?

rValue =>如果你不能获取快递的地址,那么它就是rValue。

a = 10;

仍有一些例外。例如,数组类型是一个lValue,其地址不能被获取或赋值。

int a[5] ;  // &a => Not valid
a = /* some thing */  // Not valid

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
无力看清 2024-12-08 15:09:05

你们是对的,但我认为OP的问题可能还有更多内容。这也是让我很烦恼的事情。

所以

int x;

现在 x 是一个左值。但x到底是什么?它是内存中可以存储东西的地方吗?但是等等...这将是 &x,它不是左值。

另一个奇怪的事情是,x 甚至不必在内存中拥有一个位置。编译器可能会选择始终将其保留在寄存器中。 现在是什么使它成为左值?

我认为最好的总结方式是,左值是编译器使用的一个概念,但它不会出现在运行时中。 x 可能是一个左值,因为编译器知道它可以

store $x %ax

(我编写了汇编语法;太长了)

或者它可能在寄存器中并且它知道它可以

move %bx %ax

基本上,它是一个左值,因为编译器知道如何将某些东西存储在“它”中,但你无法抓住那个“它”。

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

┼── 2024-12-08 15:09:05

需要明确的是:
首先,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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文