5
- Which means the memory address that appears with
%pand the%dexactly? - What’s the difference?
int main()
{
int teste = 10;
int *ptr = &teste;
printf("%p\n%d",ptr, ptr);
return 0;
}
5
%p and the %d exactly?int main()
{
int teste = 10;
int *ptr = &teste;
printf("%p\n%d",ptr, ptr);
return 0;
}
5
The correct way to print a pointer is the %p. Some compilers require you to make a cast for void *. Documentation.
Some implementations and depending on the compiler configuration allow using the %d directly since it is known that a pointer can be converted to an integer, others only leave if you make a cast for a whole, since the %d is the placeholder for integers and not for pointers.
If you use the %p the printed value takes into account that it is a memory address and prints in the most appropriate way possible, so it should be in hexadecimal notation which is easier to evaluate an address. If you use the %d the address will be printed as a decimal number, which can be more difficult to interpret as an address, mixing concepts. The number is the same, only the way of presenting differs.
Browser other questions tagged c pointer memory formatting printf
You are not signed in. Login or sign up in order to post.