title: Using a double pointer, how do you fill a 2D with Strings?
body: <p>I am reading a text file line by line. Putting each line into a 2d array. Sometimes the program works, but usually the first element of the 2d array returns as gibberish, example: `W@??.</p>
  <p>I've looked at the other examples on StackOverflow and can't see where this code is going wrong.</p>
  <pre><code>int read_file(FILE* file, char ** strings) {
    char str[100];
    int n = 0;
    while(fgets(str, 100, file)) {
        if(n &gt; 0) {
          strings = realloc(strings, (n+1) * sizeof(char)); 
        }
        int len = strlen(str);
  
        if (len &gt; 0 &amp;&amp; str[len-1] == '\n') {
          str[len-1] = '\0';
        }
        len = strlen(str) +1;
        strings[n] = malloc(len * sizeof(char));
        strcpy(strings[n], str);
        puts(strings[n]);         //THIS PRINTS THE CORRECT OUTPUT
        n++;
    }
  
    puts(strings[0]);      //THIS PRINTS GIBBERISH, `W@??
    puts(strings[1]);
    puts(strings[2]);
  
  return n;
  }
  
      
  int main(void) {
    FILE *file;
    file = fopen(&quot;data.txt&quot;, &quot;r&quot;);
  
    char ** strings = malloc(sizeof(char*));
  
    int n = read_file(file, strings);
    fclose(file);
    
    //print_strings(strings, n);
  
  
    puts(strings[0]);      //THIS PRINTS GIBBERISH, `W@??
    puts(strings[1]);
    puts(strings[2]);
  
  
    //freeAllocatedMemory(strings, n);
    return 1;
  }
  </code></pre>