数组地址 [英] Address of an array
问题描述
int t[10];
int * u = t;
cout << t << " " << &t << endl;
cout << u << " " << &u << endl;
输出:
0045FB88 0045FB88
0045FB88 0045FB7C
u
的输出是有意义的.
我知道 t
和 &t[0]
应该具有相同的值,但是为什么 &t
也是相同的?&t 实际上是什么意思?
I understand that t
and &t[0]
should have the same value, but how come &t
is also the same? What does &t actually mean?
推荐答案
当 t
在表达式中单独使用时,会发生数组到指针的转换,这会产生一个指向数组的第一个元素.
When t
is used on its own in the expression, an array-to-pointer conversion takes place, this produces a pointer to the first element of the array.
当 t
用作 &
运算符的参数时,不会发生此类转换.&
然后显式地获取 t
(数组)的地址.&t
是一个指向整个数组的指针.
When t
is used as the argument of the &
operator, no such conversion takes place. The &
then explicitly takes the address of t
(the array). &t
is a pointer to the array as a whole.
数组的第一个元素与整个数组的开头在内存中的相同位置,因此这两个指针具有相同的值.
The first element of the array is at the same position in memory as the start of the whole array, and so these two pointers have the same value.
这篇关于数组地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!