I'm programming C using Linux gcc with -std=c89
switch. I have a variable storing a string such as:
10, 1, 2, 3
I've shown 4 integers in the above string, but the real number isn't known ahead of time. How can I extract these values into an integer array? We can allocate the memory using an upper bound of say, 8, for example. But the actual number of integers will always be <= 8.
Currently, I'm doing the following:
sscanf(csv_variable, "%i,%i,%i,%i,%i,%i,%i,%i",
&int_variable[0],
&int_variable[1],
&int_variable[2],
&int_variable[3],
&int_variable[4],
&int_variable[5],
&int_variable[6],
&int_variable[7]);
but this works for input strings having 8 integers. Would like to have the parsing done inside a loop somehow so it can accomodate any number up to, for example, 8 possible integers (so that it works for cases where less than 8 integers are provided).