You're busy invoking undefined behaviour because you're storing a pointer to to_strip
in the reallocated array each time, and the pointer goes out of scope each iteration of the loop, and gets overwritten on each iteration which is why you see the same value at the end. If you printed all the values in the loop rather than just the current value, you'd see first 1
, then 2 2
, then 3 3 3
, then 4 4 4 4
and finally 5 5 5 5 5
. If you did enough work after returning from this function before printing the results, you'd see garbage as the space would be used for other purposes.
You need to make copies of the lines you store in your reallocated array. The simplest is to use strdup()
:
res[nums - 1] = strdup(to_strip);
Don't forget to release the strings as well as the array of pointers to the strings.
Don't forget to close the file you opened before you return.
It seems odd to pass line
into the function. It must either be a null pointer or pointing to space that can be passed to realloc()
. Since you then free()
the space before returning, the calling function needs to know that you've released the space it passed you — and since you didn't know how big it was, you told getline()
it was of size zero, so it had to be released. The interface would be cleaner without that parameter; use a local char *line = 0;
at the start of the function.