C指针的打印值有奇怪的结果
发布时间:2020-12-16 03:14:30 所属栏目:百科 来源:网络整理
导读:当我编译并运行这个C代码时,我没有得到我预期的输出. #include iostreamusing 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代码时,我没有得到我预期的输出.
#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的值打印为一行随机字符. 请有人解释为什么它不正确打印char数组的指针. 谢谢 解决方法
运算符<<对于std :: ostream和std :: wostream以特殊方式定义为char指针(char *,const char *,wchar_t *和const wchar_t *)打印出一个以空值为终止的字符串,这使您能够写
const char* str = "Hello,World"; std::cout << str; 并看到一个漂亮的字符串在你的标准. 要获取指针值,转换为void * std::cout << static_cast<void*>(c) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |