为什么整个字符串显示为结果?为什么第一个字符的地址没有被打印?如何打印第一个字符的地址?请帮我.
#include <iostream>
int main()
{
char x[6]="hello";
std::cout<<&x[0];
}
Run Code Online (Sandbox Code Playgroud)
在<<操作上std::cout会对待char*作为空终止字符串.您需要将其强制转换void*为打印指针值.
试试这个:
#include <iostream>
int main()
{
char x[6] = "hello";
std::cout << static_cast<void*>(x);
}
Run Code Online (Sandbox Code Playgroud)