 <p>Wrong reallocation.</p> <pre><code>// filename = realloc(filename, i+1); filename = realloc(filename,(i+1) * sizeof *filename);` </code></pre> <p>BTW: No need to differentiate memory allocation calls. Since the pointer is initialized to <code>NULL</code>, use <code>realloc()</code> the first and subsequent times.</p> <pre><code>char **filename = NULL; ... // if (i == 0) filename = malloc(sizeof(char*)*(i+1)); // else filename = realloc(filename,i+1); filename = realloc(filename,(i+1) * sizeof *filename);` </code></pre> <p>@PaulMcKenzie well points out code should check for problem return values. Further, <code>fileanme</code> should be <code>free()</code> in the end.</p>