I get warning: format '%lu' expects argument of type 'long unsigned int', but argument 4 has type 'long unsigned int *' [-Wformat]
for the code below
unsigned long buf[254];
char systemlogmsg[500]= {'\0'};
snprintf(systemlogmsg, sizeof(systemlogmsg), "INFO: %lu ", buf);
what should I have used instead of %lu
?
Furthermore, I tried to use
snprintf(systemlogmsg, sizeof(systemlogmsg), "INFO: %lu ", (unsigned long int *) buf);
but it did not help. How should I have passed the cast?
I think the issue here is that buf
is an array of unsigned long
s, but you're passing in a formatting specifier that expects a single unsigned long
. You should either select the specific unsigned long
out of the array that you want to print, or change the formatting specifier to print out a pointer rather than an unsigned long
.
Hope this helps!
buf
is an array of 254 unsigned long
s and decays into unsigned long *
when passed to a function. You have to decide which element of the array you want to print and use array subscripting:
snprintf(systemlogmsg, sizeof(systemlogmsg), "INFO: %lu ", buf[0]);
for example.
Edit: if you want to print out all the 254 numbers, then you need to call the function in a loop (the array index being the loop variable, obviously).
unsigned long buf[254];
declares an array of unsigned longs. So buf
is a pointer as far as sprintf is concerned. But you are trying to format %lu
, which expects not a pointer, perhaps an element of the array:
snprintf(systemlogmsg, sizeof(systemlogmsg), "INFO: %lu ", buf[0]); // ok
So, if you have 254 unsigned longs, you need to decide which one to print. If you want to print them all:
int s = 0, i;
for (i=0; i< 254; i++)
s+=snprintf(systemlogmsg+s,
sizeof(systemlogmsg)-s, "INFO: %lu ", buf[i]); // ok
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.