Printing a void* variable in C

前端 未结 3 715
再見小時候
再見小時候 2021-02-01 05:55

Hi all I want to do a debug with printf. But I don\'t know how to print the \"out\" variable.

Before the return, I want to print this value, but its type is void* .

相关标签:
3条回答
  • 2021-02-01 06:11

    This:

    uint8_t *b = (uint8_t*) out;
    

    implies that out is in fact a pointer to uint8_t, so perhaps you want to print the data that's actually there. Also note that you don't need to cast from void * in C, so the cast is really pointless.

    The code seems to be doing hex to binary conversion, storing the results at out. You can print the i generated bytes by doing:

    int j;
    for(j = 0; j < i; ++j)
      printf("%02x\n", ((uint8_t*) out)[j]);
    

    The pointer value itself is rarely interesting, but you can print it with printf("%p\n", out);. The %p formatting specifier is for void *.

    0 讨论(0)
  • 2021-02-01 06:21
    printf("%p\n", out);
    

    is the correct way to print a (void*) pointer.

    0 讨论(0)
  • 2021-02-01 06:35

    The format specifier for printing void pointers using printf in C is %p. What usually gets printed is a hexadecimal representation of the pointer (although the standard says simply that it is an implementation defined character sequence defining a pointer).

    0 讨论(0)
提交回复
热议问题