我无法弄清楚数组名称的意义
I can't figure out the point about the array name
我最近在这里发布了一些关于数组名称的问题。
感谢很多人,现在我知道数组名不是指针,也知道数组名是不可修改的l-value。
我还有一个关于数组的问题。
- 我被告知数组名被转换为数组第一个元素的地址。
所以我明白数组名是这样返回的
int arr[10];
arr == &arr[0];
但是如果是对的,不就是一个l-value因为返回的值是一个地址值吗?
我还是误会了吗?
I was told that the array name is converted into the address of the first element of the array.
这适用于对出现数组标识符的表达式求值的一部分,并且有少数例外。
So I understand that the array name is returned like this.
int arr[10];
arr == &arr[0];
自动转换为指针——发生时——类似于你的表达式&arr[0]
(实际上首先取决于该转换), 但将其视为在声明数组时立即并永久执行的转换是不正确的。数组的标识符标识 数组 ,而不仅仅是它的地址或它的第一个元素。特别是这里自动转换为指针因子的例外情况,特别是如果 arr
是数组的标识符,那么 sizeof(arr)
计算为该数组的大小,而不是大小一个指针。
But if it is right, isn't it an l-value because the returned value is an address value?
不。数组标识符是左值的原因是它指定了数组。
根据C标准(6.3.2其他操作数)
- ... A modifiable lvalue is an lvalue that does not have array type, does not
have an incomplete type, does not have a
constqualified type, and if it is a structure or union, does not have
any member (including, recursively, any member or element of all
contained aggregates or unions) with a constqualified type.
和
3 Except when it is the operand of the sizeof operator or the unary &
operator, or is a string literal used to initialize an array, an
expression that has type ‘‘array of type’’ is converted to an
expression with type ‘‘pointer to type’’ that points to the initial
element of the array object and is not an lvalue. If the array object
has register storage class, the behavior is undefined.
因此这个表达式语句
arr == &arr[0];
看起来像
&arr[0] == &arr[0];
并根据此引用(6.5.3.2 地址和间接运算符)
3 The unary & operator yields the address of its operand....and the
result is not an lvalue.
也就是在这个表达式语句中
arr == &arr[0];
int[10]
类型的不可修改左值被隐式转换为指向其第一个元素(即 int *
类型)的指针,从而获得右值。
我最近在这里发布了一些关于数组名称的问题。
感谢很多人,现在我知道数组名不是指针,也知道数组名是不可修改的l-value。
我还有一个关于数组的问题。
- 我被告知数组名被转换为数组第一个元素的地址。
所以我明白数组名是这样返回的
int arr[10];
arr == &arr[0];
但是如果是对的,不就是一个l-value因为返回的值是一个地址值吗?
我还是误会了吗?
I was told that the array name is converted into the address of the first element of the array.
这适用于对出现数组标识符的表达式求值的一部分,并且有少数例外。
So I understand that the array name is returned like this.
int arr[10]; arr == &arr[0];
自动转换为指针——发生时——类似于你的表达式&arr[0]
(实际上首先取决于该转换), 但将其视为在声明数组时立即并永久执行的转换是不正确的。数组的标识符标识 数组 ,而不仅仅是它的地址或它的第一个元素。特别是这里自动转换为指针因子的例外情况,特别是如果 arr
是数组的标识符,那么 sizeof(arr)
计算为该数组的大小,而不是大小一个指针。
But if it is right, isn't it an l-value because the returned value is an address value?
不。数组标识符是左值的原因是它指定了数组。
根据C标准(6.3.2其他操作数)
- ... A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type, does not have a constqualified type, and if it is a structure or union, does not have any member (including, recursively, any member or element of all contained aggregates or unions) with a constqualified type.
和
3 Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.
因此这个表达式语句
arr == &arr[0];
看起来像
&arr[0] == &arr[0];
并根据此引用(6.5.3.2 地址和间接运算符)
3 The unary & operator yields the address of its operand....and the result is not an lvalue.
也就是在这个表达式语句中
arr == &arr[0];
int[10]
类型的不可修改左值被隐式转换为指向其第一个元素(即 int *
类型)的指针,从而获得右值。