在C++中使用cout
时,如何后面跟的指针类型为char *
或者字符串名称,会默认输出其存储的内容,那如果在日常调试中,我们需要输出指针的地址或者字符串的地址,需要怎么做呢?
这时候就需要用到强制类型转换了,如下程序所示:
char animal[20] = "bear";
char* ps = animal;
...
cout << animal " at " << (int*) animal <<endl;
cout << ps << " at " << (int*) animal <<endl;
会输出以下结果:
bear at 0x0065fd30
bear at 0x0065fd30