Question

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.

Was it helpful?

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

OTHER TIPS

I agree with Kerrek, but I think

printf ("%s", *pa);

may not work as is since we are not sure if there is a NULL character at the end. So to print we can do the following

char temp[10+1];
memcpy(temp, *pa, 10);
temp[10] = '\0';
printf("%s",temp);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top