Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing number in array into command for function

Tags:

c

I have an array (array) that contains some char that are numbers:

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

I want to include this numbers for calling a function in the following way:

for array[0], the message to send would be "R 20". For array[1], it would 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 do I get the "R array[0]" to be "R 20".

Thanks in advance!

like image 552
Ignacio Garcia Avatar asked Feb 15 '26 04:02

Ignacio Garcia


1 Answers

sprintf it to a sufficiently sized buffer 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]); 
like image 112
PSkocik Avatar answered Feb 16 '26 17:02

PSkocik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!