When you assign the strings:
string_array[i] = temp;
you just store a pointer of the temporary buffer in each string, which will be overwritten after the next string is read. In other words, all of your strings have the same value, namely temp
, which has the contents of the last string read.
If you want to store your strings, you must allocate memory for string_array[i]
and then copy the contents with strcpy
. Alternatively, you can use a shortcut:
string_array[i] = strdup(temp);
Note that strdup
allocates memory internally that you must free()
at some point.