u_char (*)[10] & char (*)[10] format specifiers in C
-
26-05-2021 - |
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:
u_char(*) [10]
(I tried %s, but didn't work)char(*) [10]
(I tried %s and %c, but didn't work)
Thanks.
La 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])
).
Autres conseils
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);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow