Search code examples
cformat-specifiers

u_char (*)[10] & char (*)[10] format specifiers in C


I have to correct an existing C file which has a bunch of format specifiers compilation errors. Can anyone tell the correct format specifiers for the following cases:

  1. u_char(*) [10] (I tried %s, but didn't work)

  2. char(*) [10] (I tried %s and %c, but didn't work)

Thanks.


Solution

  • Both are pointers to arrays, so you can dereference them to become arrays, which decay to pointers-to-first-element:

    char arr[10];
    
    char (*pa)[10] = &arr;
    printf("%s", *pa);   // or &((*pa)[0])
    

    To spell it out: the type of pa is char(*)[10], and the type of *pa is char[10], and the latter decays to a char* of value &((*pa)[0]) (equal to &(arr[0])).