I was wondering if there is a better way to do this; let's say we read stdin for a string using fgets(), where the string contains a total of integers n(for example, 5 16 2 34for n = 4), what would be the best way to extract them? There should be a better way than this hack:
for (i=0, j=0; i<n; ++i, j+=pos)
sscanf(string+j, "%d%n", &array[i], &pos); // Using %n makes me feel dirty
I know that it is possible (and seemingly simpler) to just use something like for (i=0;i<n;++i) scanf("%d", array+i);, but I would prefer not to use it scanf()for obvious reasons. ยน
source
share