Howdy @moors7,
Yes’s sure I’m glad to help out.
Let’s say you want to parse a list of numbers separated by commas:
const int value_count = 8; // the maximum number of values
int values[value_count]; // store the values here
char string[50];
strcpy(string, "123,456,789"); // the string to split
int idx = 0;
for (char* pt=strtok(string, ","); pt && idx<value_count; pt=strtok(NULL, ",")) {
values[idx++] = atoi(pt);
}
At the end of the code, the values array looks like this:
values[0] = 123;
values[1] = 456;
values[2] = 789;
And idx
equals 3, so you know there are 3 values in the array, just in case the user passes in more or fewer values.