I've been trying to implement a function that converts something like the following:
12 13 3 4 0
into an integer array, clearing the whitespaces: int array[size] = {12,13,3,4,0};
Code (there's 2 implementations I've tried):
void string_to_int(char *line, int array[], int size) {
int i = 0;
// First implementation:
char *ptr = line;
while (*ptr != '\n')
{
sscanf(ptr, "%d%n", &array[i], &offset);
ptr += offset;
i++;
}
// Second implementation:
for(i = 0; i < size; i++)
{
array[i] = line[2*i] - '0';
}
}
The first implementation kinda worked in a way, but I get valgrind errors in the sscanf line. The second, doesn't work with numbers >10