0
votes

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?

3
What exactly do you want to do, print the 254 values, the first value, The address of the array?Musa
I want to print all 254 valuessven

3 Answers

2
votes
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
2
votes

I think the issue here is that buf is an array of unsigned longs, 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!

2
votes

buf is an array of 254 unsigned longs 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).