当我编译并运行这个C++代码时,我没有得到我期望的输出。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;
}
在UNIX机器上,我得到
i 0x967f008
l 0x967f018
c
f 0x967f090
在Windows机器上对C打印值作为经过一条线路的随机字符。
请有人可以解释为什么它不正确地打印字符数组的指针。
感谢
是什么做的C++方式:'(无效*)C'? –
这将是'reinterpret_cast(c)'。 –
Mat
@hexa:尽管@Mat是正确的,因为'(void *)c'等价于'reinterpret_cast(c)',所以* C++的做法是'static_cast (c)'; –