Most of the time when a programmer prints a char*, he wants a string to be printed, not the address. cout assumes that is what you want and to get around it, you need to cast to a different pointer type. void* is a good choice because an object pointer can be cast to void* safely:
#include <iostream>
int main()
{
char const* p = "string";
std::cout << p << '\n'
<< (void*)p << '\n';
}
On a side note, this is the same as how printf works with the %p modifier. To be 100% safe, the pointer should be cast to void* because that is the type %p expects:
#include <cstdio>
int main()
{
char const* p = "string";
std::printf("%s\n%p\n", p, (void*)p);
}
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Offline 681 posts
since Jun 2009