27

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* .

int 
hexstr2raw(char *in, void *out) {
    char c;
    uint32_t i = 0;
    uint8_t *b = (uint8_t*) out;
    while ((c = in[i]) != '\0') {
        uint8_t v;
        if (c >= '0' && c <= '9') {
            v = c - '0';
        } else if (c >= 'A' && c <= 'F') {
            v = 10 + c - 'A';
        } else if (c >= 'a' || c <= 'f') {
            v = 10 + c - 'a';
        } else {
            return -1;
        }
        if (i%2 == 0) {
            b[i/2] = (v << 4);
            printf("c='%c' \t v='%u' \t b[i/2]='%u' \t i='%u'\n", c,v ,b[i/2], i);}
        else {
            b[i/2] |= v;
            printf("c='%c' \t v='%u' \t b[i/2]='%u' \t i='%u'\n", c,v ,b[i/2], i);}
        i++;
    }
    printf("%s\n", out);
    return i;
}

How can I do? Thanks.

sharkbait
  • 2,980
  • 16
  • 51
  • 89
  • 1
    What kind of data are you expecting `out` to point to? – Nick Mar 08 '13 at 10:53
  • 2
    Do you really want to print the pointer value itself? Considering what the code does, it seems more likely that you want to print the result, i.e. the generated bytes. See my answer for how to do that. :) – unwind Mar 08 '13 at 10:58

3 Answers3

63
printf("%p\n", out);

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

Graham Borland
  • 60,055
  • 21
  • 138
  • 179
  • 7
    If you want to print the value rather than the pointer, then see unwind's answer. – Graham Borland Mar 08 '13 at 11:12
  • I stored a constant character sequence in a void* and using %p% it is not printing the string but what appears to be the address of the string – Mushy Sep 26 '17 at 19:46
25

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 *.

unwind
  • 391,730
  • 64
  • 469
  • 606
6

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).

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Technically, it's up to the implementation to choose the "what gets printed", but I've never actually seen anything other than a simple hex number (despite rumours about AS/400s). :-) – torek Mar 08 '13 at 10:53
  • @torek You're right, the standard says it's implementation defined. All implementations I've seen thus far decided to use hex, though :) – Sergey Kalinichenko Mar 08 '13 at 10:56
  • 1
    @dasblinkenlight Hi, I am a little late here, but was hoping to ask you: I have seen from somewhere that printf("%p\n", ptr); should be printf("%p\n", (void *) ptr); or else we would get undefined behavior, is it true? If so, why? Thanks in advance. – Unheilig Jan 07 '14 at 18:43
  • 1
    @Unheilig Yes, this is true, unless `ptr` is a `char*`/`unsigned char*`, which are required by the standard to have the same representation as `void*`. Pointers to any other type may have a different layout in memory (this is rare, but it may happen). That is why an explicit conversion is required. – Sergey Kalinichenko Jan 07 '14 at 18:48