c - 为什么 C 中没有数组类型的右值?

标签 c

我在一次讨论中读到了这一声明:

There are no rvalues of array type in C. There are pointer lvalues and rvalues, integer lvalues and rvalues, structure lvalues and rvalues etc... But only lvalue arrays. When you try to convert an lvalue of array type to an rvalue, you no longer have an array, you have a pointer to the array's first member.

当我们无法更改数组的基地址时,我无法理解这一点,那么我们如何将它用作左值,如何在赋值语句的左侧使用它的名称?

请举例简单说明。

最佳答案

C11标准草案n1570 footnote 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.

因此,数组的名称是一个对象定位器值,因为它本身就是一个对象。然而,在值上下文中,数组的名称会衰减为指向第一个元素的指针。

如果我没记错的话,甚至右值 - 即struct类型的表达式的值也是后来(ANSIsh之前)添加到该语言中的。这可以通过向后兼容的方式来完成,因为结构不会退化为指针,但它不适用于数组。

关于c - 为什么 C 中没有数组类型的右值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43281421/

相关文章:

c - 使用指针加密字符串的函数

c - 如何添加仅允许 a-f || 之间的字母的 "(if)"A-F?

c - 隐式声明出错,但函数在源文件中声明

c - 我正处于解决这个问题的边缘。看看...谁能告诉我我应该改变什么?

c - format ‘%f’ 需要类型为 ‘float *’ 的参数,但参数 2 的类型为 ‘double *’ [-Wformat=] scanf ("%1f", &n);

c - 在Linux用户空间进程中,vsyscall页面的地址是什么?

c++ - 如何在 C/C++ 中对二维数组求和

c - 多维数组模式访问

c - 如何在调用 strcpy 之前分配数组?

c - malloc、sizeof 和 strlen 函数可能发生冲突吗?