什么是左值和右值?

What are lvalues and rvalues?

我听说在使用指针时会出现术语 lvaluervalue
但是,我并没有完全理解他们的意思。

什么是左值右值

注1:这是C的左值和右值的问题,不是C++的。这也是关于他们的功能,而不是他们的命名

注2:我已经完全理解这些概念了。这是一个典型的重复目标。

我有一个更长的答案here, but basically C11 draft n1570 6.3.2.1p1:

An lvalue is an expression (with an object type other than void) that potentially designates an object [...]

C11 n1570 Footnote 64:

64) The name lvalue comes originally from the assignment expression E1 = E2, in which the left operand E1 is required to be a (modifiable) lvalue. It is perhaps better considered as representing an object locator value. What is sometimes called rvalue is in this International Standard described as the value of an expression. An obvious example of an lvalue is an identifier of an object. As a further example, if E is a unary expression that is a pointer to an object, *E is an lvalue that designates the object to which E points.

并非所有左值都是可修改的,即可以出现在赋值的左侧。不可修改的左值的例子是

  • 有数组类型,
  • 类型不完整
  • 是 const 限定的
  • structs 或 unions 直接或递归地具有 const 合格成员

可以通过左值转换将左值转换为表达式的值。 IE。在

int a = 0, b = 1;
a = b;

ab 都是左值,因为它们可能 - 并且实际上 - 指定对象,但是 b 在赋值的右侧进行左值转换,表达式 b 左值转换后的值为 1.

"Potentially designating an object"表示给定int *p;,*p指定一个int类型的对象iff p 指向 int 类型的对象 - 但 *p 是左值,即使 p == NULL 或不确定。

根据C 参考手册(第 3 版)

An lvalue is an expression that refers to an object in such a way that the object may be examined or altered. Only an lvalue expression may be used on the left-hand side of an assignment. An expression that is not an lvalue is sometimes called an rvalue because it can only appear on the right-hand side of an assignment. An lvalue can have an incomplete array type, but not void.