如何在2D char数组中打印[x] [y]元素的地址?(C++)

EmJ*_*Jov 3 c++ arrays char

char arr[2][6] = { "hello", "foo" };

cout << arr[0] << " or " << *(arr) << endl;// prints "hello"
cout << arr[1] << " or " << *(arr + 1) << endl; // prints "foo"

cout << arr << endl; // prints an address of "hello" (and 'h')
cout << arr + 1 << endl; //prints an address of "foo" (and 'f')

cout << arr[0][1] << endl; // prints 'e'
cout << &arr[0][1] << endl; // prints "ello"
Run Code Online (Sandbox Code Playgroud)

所以,我想在"你好"中打印一个"e"的地址.我怎么做?

我知道如果我正在处理任何其他类型的数组,&arr [0] [1]会完成这项工作,但是所有这些cout char(数组)重载我不确定它是否可能?

Jar*_*d42 5

有一个重载operator <<(std::ostream&, const char*)打印c-string(以空字符结尾的字符串).void*在这种情况下你必须转换为打印地址:

std::cout << static_cast<const void*>(&arr[0][1]) << std::endl;
Run Code Online (Sandbox Code Playgroud)