I am using the following piece of code as a learning exercise for pointers:
int a=8;
int *ptr=&a;
printf("\nThe & address of a is: %x\n",&a);
printf("\nThe value of the pointer ptr is: %p \n",ptr);
I am doing this to identify the address values given by &a and ptr and I am noticing the following difference in the output:
The & address of a is: a6bff9c4
The value of the pointer ptr is: 000000f5a6bff9c4
I can see that the ptr value is the & value with 000000f5 appended in the beginning. I know that %x outputs the & address value in hexadecimal. What is the format of the pointer value and how is it different from the hexadecimal & value?
Trying to understand the difference between the memory addresses outputted by & and pointer variable and understanding their formats.
Using the %x format specifier truncates part of the data you are attempting to display. %x is essentially designed to format an int in its hexadecimal form, typically with room for 4 bytes, but being asked to represent a pointer that in this case (but not always) occupies more space than an int
The %p format specifier is specifically designed to accommodate depictions of pointers. Depending on the addressing of the system the pointer is referencing, it will be either 4 bytes (32bit) or 8 bytes (64bit)
Thus a6bff9c4 is erroneously depicted with %x, and should be depicted using %p.
The conversion specification %x expects an object of the type unsigned int. sizeof( unsigned int ) usually is equal to 4. While sizeof( int * ) is equal to 4 or 8 bytes depending on the used system.
Using invalid conversion specification results in undefined behavior.
From the C Standard (7.21.6.1 The fprintf function)
9 If a conversion specification is invalid, the behavior is undefined.275) If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.
Instead of this call where with a pointer there is used the conversion specification %x
printf("\nThe & address of a is: %x\n",&a);
you should write
#include <inttypes.h>
//...
printf("\nThe & address of a is: %08" PRIXPTR "\n", ( uintptr_t ) ( void * )&a);
you need to write
And in the second call of printf you need to convert the pointer to the type void *.
Here is a demonstration program.
#include <stdio.h>
#include <inttypes.h>
int main( void )
{
int a = 8;
int *ptr = &a;
printf( "\nThe & address of a is: %08" PRIXPTR "\n", ( uintptr_t )( void * )&a );
printf( "\nThe value of the pointer ptr is: %p \n", ( void * )ptr );
}
The program output might look like
he & address of a is: 0116F9BC
The value of the pointer ptr is: 0116F9BC
This call of printf
printf( "\nThe & address of a is: %08" PRIXPTR "\n", ( uintptr_t )( void * )&a );
can be more flexible if to rewrite it the following way
printf( "\nThe & address of a is: %0*" PRIXPTR "\n", 2 * ( int )( sizeof( &a ) ), ( uintptr_t )( void * )&a );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With