gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 18:36:02 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com