我正在读一本 C++ 的书,我正在做一些测试,因为我不明白一些东西,当我尝试打印这 3 行时,我期望所有 3 行都有不同的东西。第一个,指针在内存中的位置,第二个,指针存储的地址,第三个,指针存储的地址处的值。但前两个是相同的:
cout <<&arey <<"\n";
cout <<arey <<"\n";
cout <<*arey <<"\n";
输出:
0x6afee8
0x6afee8
0
谁能解释为什么我读到数组的名称是指向数组的指针,所以如果它是指针,它应该有自己的内存地址吗?
I dont understand how an arrays name is a pointer to the array
不是。
I have read that the name of the array is a pointer to the array
写这篇文章的人都在骗你。
数组的名字就是数组的名字。
Can someone explain why
在某些条件下(例如,将其传递给函数),它会自动(且透明地)转换为指向数组第一个元素的指针。这是我们从 C 继承的一个不幸的事实。
但数组的名称仍然只是数组的名称。
所以:
arey An array
&arey Address of the array
f(&arey) Passing the address of the array
f(arey) Still passing the address of the array (ish) - thanks a lot, C!
f(*arey) Passing the first element's value
现在考虑 cout << x
实际上是 cout.operator<<(x)
(即函数调用)并解释您的输出。
if it is a pointer it should have its own memory address right?
如果它是一个指针,那么,是的,它会。