Why / when to use `intptr_t` for type-casting in C?

前端 未结 4 1269
长发绾君心
长发绾君心 2020-11-28 03:13

I have a question regarding using intptr_t vs. long int. I\'ve observed that incrementing memory addresses (e.g. via manual pointer arithmetic) dif

4条回答
  •  孤街浪徒
    2020-11-28 03:57

    You could make your life easier by using the p conversion specifier:

    printf("%p\n", (void *)foo);
    

    Also, the portable way to print a variable of type (u)intptr_t is to use the PRI*PTR macros from inttypes.h; the following is equivalent to using p on my platform (32-bit):

    printf("%08" PRIxPTR "\n", (uintptr_t)(void *)foo);
    

    The casts to void * are necessary for full portability, but can be omitted on platforms with uniform pointer representations.

提交回复
热议问题