我正在测试指针和数组时,遇到了困惑。以下是我测试过的代码:
#include <iostream>
using namespace std;
int main(void) {
char a[] = "hello";
cout << &a[0] << endl;
char b[] = {'h', 'e', 'l', 'l', 'o', '\0'};
cout << &b[0] << endl;
int c[] = {1, 2, 3};
cout << &c[0] << endl;
return 0;
}
我原本期望这段代码会打印出三个地址(即a[0]、b[0]和c[0]的地址)。但实际结果是:
hello
hello
0x7fff1f4ce780
为什么使用 char 类型时,对于前两种情况,'&' 返回整个字符串?我是否漏掉了什么重要的信息?