[英]Is the address of an array equal to its first element in C
Assume this array:假设这个数组:
int arr[5] = { 1, 2, 3, 4, 5 };
Here arr
has two purpose - it is the name of the array and it acts as a pointer pointing towards the first element in the array, so arr
is equal to &arr[0]
by default.这里
arr
有两个用途——它是数组的名称,它充当指向数组中第一个元素的指针,因此默认情况下arr
等于&arr[0]
。
My questions is, what about &arr
?我的问题是,
&arr
呢? is this also equal to the above?这也等于上面的吗?
An array is in fact an extent of memory.数组实际上是内存的一个范围。 So the address of an extent is the address of the stored array in this extent and of its first element.
因此,范围的地址是该范围中存储的数组及其第一个元素的地址。
As a result an array designator used in expressions with rare exceptions is converted to pointer to its first element.因此,在具有罕见异常的表达式中使用的数组指示符被转换为指向其第一个元素的指针。
Using the example provided by you使用您提供的示例
int arr[5] = { 1, 2, 3, 4, 5 };
these expressions &arr
and &arr[0]
have the same value but are of different types.这些表达式
&arr
和&arr[0]
具有相同的值但类型不同。
The expression &arr
has the type int( * )[5]
while the expression &a[0]
has the type int *
.表达式
&arr
的类型为int( * )[5]
而表达式&a[0]
的类型为int *
。
Here is a demonstrative program.这是一个演示程序。
#include <stdio.h>
int main(void)
{
int arr[5] = { 1, 2, 3, 4, 5 };
printf( "&arr == &arr[0] is %s\n",
( void * )&arr == ( void * )&arr[0] ? "true" : "false" );
printf( "sizeof( *&arr ) = %zu\n", sizeof( *&arr ) );
printf( "sizeof( *&arr[0] ) = %zu\n", sizeof( *&arr[0] ) );
return 0;
}
Its output is它的输出是
&arr == &arr[0] is true
sizeof( *&arr ) = 20
sizeof( *&arr[0] ) = 4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.