sizeof(avgText)
and sizeof(maxText)
are sizes of pointers, not array sizes. See e.g. here. Change your code to
static char avgText[] = "WAIT AVG: ";
static char maxText[] = "MAX: ";
in order to make them arrays.
edit:
Further note, that sizeof(average)
is the size in bytes average
covers internally, not how many bytes a decimal representation would consume.
edit2:
As Roddy's answer says, it's wrong to calculate the size we want to have and pass that size to snprintf
as an actual buffer size. We can, however, calculate the size we want to have provided there is a reasonable upper bound (e.g. with 32 bit int
, 10 bytes (without 0-terminator) are always sufficient, but maybe you can give a lower upper bound in your use case):
char labelText [
sizeof avgText - 1 + 10 +
sizeof maxText - 1 + 10 + 3 + 1
];
/* sizeof avgText counts the 0-terminator, so does sizeof maxText, hence
the -1, two times 10 for the `int` (or any reasonable upper bound you have),
3 spaces and the 0-terminator. */
and you could even use sprintf
. With snprintf
, you can do:
snprintf(labelText, sizeof labelText,"%s %d %s %d", avgText, average, maxText, maximum);
HTH