带有char数组的c ++&符号运算符

sca*_*row 6 c++ arrays pointers

当我对我正在测试的这段代码感到困惑时,我只是在玩指针和数组.

#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;
}
Run Code Online (Sandbox Code Playgroud)

我希望这会打印三个地址(a [0],b [0]和c [0]).但结果是:

hello
hello
0x7fff1f4ce780
Run Code Online (Sandbox Code Playgroud)

为什么前两个用char的情况,'&'给出整个字符串或者我错过了什么?

Luc*_*ore 10

由于coutoperator <<打印字符串,如果你传递一个char*作为参数,这是什么&a[0]是.如果要打印地址,则必须明确地将其转换为void*:

cout << static_cast<void*>(&a[0]) << endl;
Run Code Online (Sandbox Code Playgroud)

要不就

cout << static_cast<void*>(a) << endl;
Run Code Online (Sandbox Code Playgroud)