 <p>The only buffer you ever created for holding strings was <code>t</code>, which you made at the top of your while loop:</p> <pre><code>char t [KMAX]; </code></pre> <p>So all of your <code>char *</code> pointers will be pointing somewhere into that buffer, but that's a problem because you change the contents buffer every time you call <code>fgets</code>. After you are done reading the input, the only text you actually have stored in RAM will be the data from the last call to <code>fgets</code>.</p> <p>You could change the <code>add</code> function so it allocates new buffers and copies the strings to them. There is already a function that does that for you and it is called <a href="http://linux.die.net/man/3/strdup" rel="nofollow">strdup</a>. Something like this would work (though I have not tested it):</p> <pre><code>void add(struct info **x, char * text, int line_no, char * file) { ... newInfo = malloc(sizeof(struct info)); newInfo-&gt;next = NULL; newInfo-&gt;grepstring = strdup(text); newInfo-&gt;line_no = line_no; newInfo-&gt;file = strdup(file); ... } </code></pre>