Printing uint64_t* value in C

1.7k views Asked by At

I am using the following code:

    size_t offset = (rand() << 12) % chunk_size;
    uint64_t *addr = (uint64_t*) (chunk+offset);
    fprintf(stdout,"addr: %" PRIx64 " ", addr);

I included <inttypes.h> library but i got the folowwing error:

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

why?

thank you.

1

There are 1 answers

1
Zig Razor On BEST ANSWER

The problem is that the type you want to print is an uint64_t * and not a uint64_t, so if you want to print the pointer(addr) just use %p in the printf, else you need to dereference the pointer using the *.