Boris Month
Boris Month

Reputation: 463

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?

Upvotes: 4

Views: 4328

Answers (3)

Steztric
Steztric

Reputation: 2942

How about just

cout << &c << endl;

Works for me :)

Upvotes: 0

Component 10
Component 10

Reputation: 10477

Try casting it as a const void* :

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

Upvotes: 1

YePhIcK
YePhIcK

Reputation: 5856

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

or just

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

Upvotes: 8

Related Questions