打印char数组的地址

Bor*_*nth 3 c++ cout char ostream

int *i = new int(1);
cout << i << endl; 
Run Code Online (Sandbox Code Playgroud)

将打印整数的地址.

    char *c="cstring";
    cout << c << endl;
    cout << &(*c) << endl;
Run Code Online (Sandbox Code Playgroud)

两者都会打印"cstring".我想这个行为可以简单地通过ostream& operator<< (ostream& out, const char* s );IOstream库中的实现来解释.

但是,如果你真的想要打印数据c的地址怎么办呢?

YeP*_*IcK 7

cout << reinterpret_cast<void*>(c) << endl;
Run Code Online (Sandbox Code Playgroud)

要不就

cout << (void*)c << endl;
Run Code Online (Sandbox Code Playgroud)