C++指针的打印值给出了奇怪的结果

Fre*_*die 9 c++ pointers

当我编译并运行这个C++代码时,我没有得到我期望的输出.

#include <iostream>
using namespace std;

int main()
{
    int * i = new int;
    long * l = new long;
    char * c = new char[100];
    float * f = new float[100];

    cout << "i " << i << endl;
    cout << "l " << l << endl;
    cout << "c " << c << endl;
    cout << "f " << f << endl;


    delete i;
    delete l;
    delete []c;
    delete []f;

    cin.get();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在unix机器上,我得到了

i 0x967f008
l 0x967f018
c
f 0x967f090
Run Code Online (Sandbox Code Playgroud)

在Windows机器上,c的值打印为随机字符行.

请有人解释为什么它没有正确打印char数组的指针.

谢谢

Arm*_*yan 19

operator <<std::ostreamstd::wostream在特殊的方式被定义为char指针(char*,const char*,wchar_t*const wchar_t*打印出一个空终止字符串,这使您可以编写

const char* str = "Hello, World";
std::cout << str;
Run Code Online (Sandbox Code Playgroud)

并在你的标准输出上看到一个很好的字符串.

要获取指针值,请转换为 void *

std::cout << static_cast<void*>(c)
Run Code Online (Sandbox Code Playgroud)