operator<<
does not have an overload for an int*
pointer, so the expression cout << y
ends up calling the void*
overload, which prints the memory address that the pointer is pointing at. It doesn't try to access the data being pointed at. Same with printf("%x", z)
.
operator<<
does have an overload for an unsigned char*
pointer, which treats the pointer as pointing to a null-terminated string, same as the char*
overload does. But z
is not a pointer to a null-terminated string, so your code has undefined behavior when trying to print z
this way. To print the address that z
is pointing at, you need an explicit typecast to void*
, eg:
cout << static_cast<void*>(z)
That being said, the reason your code doesn't just fail outright is because the numeric value 15 (hex 0x0F) as an int
happens to contain several 0x00
bytes in it (3 of them, if int
is 4 bytes in size, as it is on most platforms). Depending on endian, the int
will be laid out in memory as either 0F 00 00 00
or 00 00 00 0F
. So, trying to print z
as a null-terminated string will encounter one of those 0x00
bytes and interpret it as a null terminator. If the 0x0F
byte is encountered first, it will simply be interpreted as an ASCII control character that has no visual representation. Either way, you won't see anything being printed to the console.