Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

print address of array of char

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?

like image 238
Boris Month Avatar asked Jan 16 '23 02:01

Boris Month


2 Answers

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

or just

cout << (void*)c << endl;
like image 181
YePhIcK Avatar answered Jan 24 '23 17:01

YePhIcK


Try casting it as a const void* :

cout << static_cast<const void*>(c) << endl;
like image 21
Component 10 Avatar answered Jan 24 '23 17:01

Component 10