print address of array of char

假如想象 提交于 2020-01-02 07:50:49

问题


int *i = new int(1);
cout << i << endl; 

Will print the address of the integer.

    char *c="cstring";
    cout << c << endl;
    cout << &(*c) << endl;

Will both print "cstring". I guess this behavior can simply be explained with the implementation of ostream& operator<< (ostream& out, const char* s ); in the IOstream Library.

But what to do if you actually want to print the address of the data c refers to?


回答1:


cout << reinterpret_cast<void*>(c) << endl;

or just

cout << (void*)c << endl;



回答2:


Try casting it as a const void* :

cout << static_cast<const void*>(c) << endl;



回答3:


How about just

cout << &c << endl;

Works for me :)



来源:https://stackoverflow.com/questions/11537621/print-address-of-array-of-char

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!