简体   繁体   English

C指针位置,十进制和十六进制

[英]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. %p格式为void *并以实现定义的格式打印。 If you want to seize control, use the types from <stdint.h> and formats from <inttypes.h> (first defined in C99): 如果要抓住控制权,请使用<stdint.h>的类型和<inttypes.h>格式(在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). uintptr_t类型(名义上是可选的,但所有实际实现都应定义它)是一个无符号整数类型,其大小足以容纳指向对象的指针(变量;不一定足够容纳函数指针)。 The names such as PRIuPTR define the right conversion specifier for the uintptr_t type (the value is platform specific). 诸如PRIuPTR的名称为uintptr_t类型定义了正确的转换说明符(该值是特定于平台的)。

Note that if you use <inttypes.h> , you don't need to include <stdint.h> . 请注意,如果使用<inttypes.h> ,则无需包括<stdint.h>

C usually returns memory addresses as hexadecimal numbers, so you only need to use %p. C通常以十六进制数返回内存地址,因此您只需要使用%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> . 不要忘记包含#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 . 如果您对unitptr_t感到不舒服,请想象您正在转换为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 . 另外,您可能想看看%p%x之间的区别

For decimal use %lu (long unsigned) instead %p Also, no need for the (void *) casting with normal printf function 对于十进制,请使用%lu(长无符号长整数)代替%p。此外,无需使用常规printf函数进行(void *)强制转换

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. 以十六进制格式打印指针时,可以使用%p代替%x。 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 ); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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