在 C 中打印 uint64_t* 值

Printing uint64_t* value in C

我正在使用以下代码:

    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,否则你需要使用*.

取消引用指针