Printing a pointer with %d
is formally undefined behavior, meaning anything can happen, including a program crash. Your program will for example likely break when you compile it as a 64 bit application, where int
is 32 bits but a pointer is likely 64 bits. Therefore, always use %p
and never anything else when printing a pointer.
There is no implicit conversion taking place - the printf family of functions doesn't have that kind of intelligence - it doesn't know the type passed. With the format specifier, you tell the function which type it is getting. And if you lie to printf and say "I'll give you an int" and then give it a pointer, you unleash bugs. This makes the printf family of functions very dangerous in general.
(The only implicit conversion that take place in printf is when you pass small integer types or float, in which case the "default argument promotions" take place and promote the parameter either to int
or double
. This is not the case here, however.)
In this specific case, you happened to get the decimal representation of 0x0060FF08
, which is by no means guaranteed.
Pedantically, you should also cast the pointer to type (void*)
since this is what %p
expects.