Im not very experienced with C so this is probably a basic question. I have a script that opens up 5 sockets, it then runs through a loop and on a given event reconnects to the relevant socket and sends some data. The socket to be reconnected to is kept track of with a 'count' variable. The sockets are named s0,s1,s2 etc. This is the code I have to send the data:
if (count == 0) write(s0,"some data",9);
if (count == 1) write(s1,"some data",9);
if (count == 2) write(s2,"some data",9);
if (count == 3) write(s3,"some data",9);
if (count == 4) write(s4,"dome data",9);
Instead of this I simply want to use:
write(s+count,"some data",9)
But how do I append the count integer onto the 's'
hmmm, I was hoping I would be able to do this with sprintf or similar. In VB I could use 'cstr(count)' to cast the count integer to a string, is there nothing similar in C?
Absolutely nothing, sorry. You can't do things that way. C is not a shell. There is a difference between variable names and strings that can't be bridged -- you can't use a string as a variable name, or anything analogous, because entity names simply can't change at runtime, at all, ever.
Perderabo has the only logical solution. You wouldn't want to code all those if/else statements everywhere anyway, that's horrible coding style for this. It is obvious that you have a group of sockets, thus they should be logically grouped. An array will provide a group of sockets and you can access them as such.