The overload of operator<<
, used with std::cout
and char*
as arguments, is expecting a null-terminated string. What you are feeding it with, instead, is a pointer to what was an int*
instead. This leads to undefined behavior when trying to output the char*
in cout<<p0<<endl;
.
In C++, is often a bad idea to use C-style casts. If you had used static_cast
for example, you would have been warned that the conversion your are trying to make does not make much sense. It is true that you could use reinterpret_cast
instead, but what you should be asking yourself is: why am I doing this? Why am I trying to shoot myself in the foot?
If what you want is to convert the number to string, you should be using other techniques instead. If you just want to print out the address of the char*
you should be using std::addressof
:
std::cout << std::addressof(p0) << std::endl;