Question

I'm developing for Pebble and I'm off to a rough start.

I'm trying to populate a text_layer with 2 strings and 2 values, something like this:

WAIT AVG: 3 MAX: 5

Since malloc is not supported in Pebble SDK, I can't use sprintf, hence I'm stuck with snprintf. The following code only prints "4":

srand(time(NULL));
int average = (rand()%6)+1; 
int maximum = average + 2;
static char *avgText="WAIT AVG: ";
static char *maxText="MAX: ";
snprintf(labelText,sizeof(avgText) + sizeof(average) + sizeof(maxText) + sizeof(maximum),"%s %d %s %d",avgText,average,maxText,maximum);

Any help would be greatly appreciated. I know that I can create 4 separate TextLayers but that's kind of a last resort for me.

Was it helpful?

Solution

You're just using snprintf wrong. ;-)

The second parameter (which you're trying to calculate, badly) is the length of the character array that you're printing into, not the number of characters you're trying to write.

Something like this should work:

char labelText[64]; // Is this big enough?
snprintf(labelText, 64,"%s %d %s %d",avgText,average,maxText,maximum);

OTHER TIPS

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

read the man page of snprintf() properly. it says, the second argument size is used to refer to the number of bytes to be written.

sizeof (avgText) and sizeof(maxText)is not gonna work here. It refers to the size of the pointer, not the length of the array it holds. Maybe you want to use strlen() for the string length.

So this worked:

srand(time(NULL));
static char labelText[]="WAIT AVG: xxxxx MAX: xxxxxx";
int average = (rand()%6)+1; 
int maximum = average + 2;
static char avgText[]="WAIT AVG: ";
static char maxText[]="MAX: ";
snprintf(labelText,sizeof(labelText),"%s %d %s %d",avgText,average,maxText,maximum);
text_layer_set_text(waitField,labelText);

thanks guys

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top