Int*_*rer 4 c printf pointers memory-address format-string
我有一个int指向另一个地址的指针int。
当使用格式说明符打印指针时%p(如 Stack Overflow 上的许多答案所建议的那样),它以十六进制表示形式打印。
如何用十进制表示打印指针的值?
代码示例
我想出了一个可以%zu从这个答案中使用的人。
int i = 1;
int *p = &i;
printf("Printing p: %%p = %p, %%zu = %zu\n", p, p);
Run Code Online (Sandbox Code Playgroud)
当我使用https://onlinegdb.com/EBienCIJnm运行该代码时
warning: format ‘%zu’ expects argument of type ‘size_t’, but argument 3 has type ‘int *’ [-Wformat=]
Printing p: %p = 0x7ffee623d8c4, %zu = 140732759529668
Run Code Online (Sandbox Code Playgroud)
有没有办法printf使用十进制表示的指针值,而没有编译器警告?
转换为 中uintptr_t定义的类型<stdint.h>并使用PRIuPTR中定义的说明符进行格式化<inttypes.h>:
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("%" PRIuPTR "\n", (uintptr_t) &argc);
}
Run Code Online (Sandbox Code Playgroud)