Why does streaming a char pointer to cout not print an address?

c++, char, cout, pointers, string

Solution

Overload resolution selects the `ostream& operator<<(ostream& o, const char *c);` which is used for printing C-style strings. You want the other `ostream& operator<<(ostream& o, const void *p);` to be selected. You are probably best off with a cast here:

 cout << static_cast<void *>(cptr) << endl;

Problem

When I print a char pointer with `printf()`, it makes the decision with conversion specifier whether the address should be printed or the whole string according to %u or %s. But when I want to do the same thing with `cout`, how will `cout` decide what should be printed among address and whole string? Here is an example source: ``` int main() { char ch='a'; char *cptr=&ch; cout<<cptr<<endl; return 0; } ``` Here, in my GNU compiler, `cout` is trying to output ch as a string. How I can get address of `ch` via `cptr` using `cout`?

Original source

Related problems