简体   繁体   中英

C Pointer Location, Decimal and Hexadecimal

I am trying to learn how to display the pointer value in decimal and hexadecimal. below you can see me create a value and try to use a pointer to print out the value and the values location.

Please make the code work correctly so that it prints out the values in decimal and hexadecimal

double val= 1;
printf("The value of val : %f\n",val);


double *ptr;
ptr= &val;

printf("dereference *ptr= %f\n", *ptr);

//Display the location of val with and without pointer use in decimal and hex


//decimal
printf("location of val in decimal with ptr is: %p\n",(void *) ptr); 
printf("location of val in decimal without a pointer is: %p\n",(void *) &val ); 

//hexadecimal THIS IS NOT WORKING 
printf("location of val in hex with ptr is: %#x\n", (void *) ptr); 
printf("location of val in hex without a pointer is: %#x\n", (void *) &val ); 

The %p format takes a void * and prints it in an implementation-defined format. If you want to seize control, use the types from <stdint.h> and formats from <inttypes.h> (first defined in C99):

#include <inttypes.h>

printf("Location in decimal:  %" PRIuPTR "\n", (uintptr_t)ptr);
printf("Location in hex:      0x%.8" PRIXPTR "\n", (uintptr_t)ptr);
printf("Location in octal     %#" PRIoPTR "\n", (uintptr_t)ptr);

Etc.

The uintptr_t type (which is nominally optional, but all practical implementations should define it) is an unsigned integer type big enough to hold a pointer to an object (variable; not necessarily big enough to hold a function pointer). The names such as PRIuPTR define the right conversion specifier for the uintptr_t type (the value is platform specific).

Note that if you use <inttypes.h> , you don't need to include <stdint.h> .

C usually returns memory addresses as hexadecimal numbers, so you only need to use %p. As for the decimal representation, you can use type casting:

int rand1 = 12, rand2 = 15;

printf("rand1 = %p : rand2 = %p\n\n", &rand1, &rand2); 
// returns hexadecimal value of address

printf("rand1 = %d : rand2 = %d\n\n", (int) &rand1, (int) &rand2);  
// returns decimal value of address

Don't forget to include #include <inttypes.h> .

As suggested by the comments, it's better to do this:

//hexadecimal
printf("Location in hex:      0x%.8" PRIXPTR "\n", (uintptr_t)ptr);
printf("Location in hex:      0x%.8" PRIXPTR "\n", (uintptr_t)&val);

If you feel uncomfortable with the unitptr_t cast, then imagine you are casting to an unsigned int . It's not the same, but it's something for a start.

For more, read this answer.

Also, you might want to take a look into the difference between %p and %x .

For decimal use %lu (long unsigned) instead %p Also, no need for the (void *) casting with normal printf function

Like this:

//decimal
printf("location of val in decimal with ptr is: %lu\n",ptr); 
printf("location of val in decimal without a pointer is: %lu\n",&val );

You may use the %p instead %x when printing a pointer in hex format. Like this:

//hexadecimal
printf("location of val in hex with ptr is: %#x\n", ptr); 
printf("location of val in hex without a pointer is: %p\n", &val ); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM