Writing a number in an array to a command for a function

I have an array (array) containing several char that are numbers:

char array[] = [20, 3, 32, 34, -12] //for example

      

I want to include these numbers to call a function like this:

for array [0], the message to send will be "R 20". For array [1] this will be "R 3" ...

sendtoserver("R 20");

      

How can i do this? I know I need a "for" loop for all of them, but my question is how to get "R array [0]" as "R 20".

Thanks in advance!

+3


source to share


1 answer


sprintf

into a buffer of sufficient size and pass the buffer:



char buf[14]; 
//14 is enough for "R " (2) + 
//the decimal representation of any 32 bit int (11) + '\0' (1) 
//2 + 4 + 1  = 7 would be  enough for sized, 8 bit chars
sprintf(buf, "R %d", array[i]); 

      

+4


source







All Articles