免责声明:C++指针是一个非常流行的话题,因此我相信之前已经有人提出了这个问题。然而,我没有找到其他参考资料。如果我错了,请纠正我,并随时关闭这个线程。
我看到很多例子区分指向数组第一个元素的指针和指向整个数组本身的指针。下面是一个程序及其输出:
//pointers to arrays
#include <iostream>
using namespace std;
int main() {
int arr[10] = {};
int *p_start = arr;
int (*p_whole)[10] = &arr;
cout << "p_start is " << p_start <<endl;
cout << "P_whole is " << p_whole <<endl;
cout << "Adding 1 to both . . . " <<endl;
p_start += 1;
p_whole += 1;
cout << "p_start is " << p_start <<endl;
cout << "P_whole is " << p_whole <<endl;
return 0;
}
输出:
p_start is 0x7ffc5b5c5470
P_whole is 0x7ffc5b5c5470
Adding 1 to both . . .
p_start is 0x7ffc5b5c5474
P_whole is 0x7ffc5b5c5498
所以,不出所料,对两个数都加1会得到不同的结果。但我不知道像 p_whole
这样的东西有什么实际用途。一旦我拥有整个数组块的地址,这可以使用 arr
来获取,那么我可以用这样的指针做什么呢?