我正在使用以下代码:
size_t offset = (rand() << 12) % chunk_size;
uint64_t *addr = (uint64_t*) (chunk+offset);
fprintf(stdout,"addr: %" PRIx64 " ", addr);
我包含了<inttypes.h>
库,但我得到了以下错误:
error: format ‘%lx’ expects argument of type ‘long unsigned int’, but argument 3 has type ‘uint64_t *’ {aka ‘long unsigned int *’} [-Werror=format=]
fprintf(stdout,"addr: %" PRIx64 " ", addr);
uint64_t * {aka long unsigned int *}
In file included from filename.c:5:
/usr/include/inttypes.h:121:34: note: format string is defined here
121 | # define PRIx64 __PRI64_PREFIX "x"
cc1: all warnings being treated as errors
make: *** [Makefile:8: filename.o] Error 1
为什么?
谢谢。
问题是要打印的类型是uint64_t *
而不是uint64_t
,因此,如果要打印指针(addr
(,只需在printf
中使用%p
,否则需要使用*
取消引用指针。