title: Array of string in C: OK within a function, full of garbage in main()
body: <p>An array of strings is created in main and passed to a function. This function reads strings from a file and stores every line of the file in a different row of the array. Then finishes and returns to main. If I printf the contents of array[i] within the function, the text displayed in the screen is OK. If I do the same after returning to main(), printf writes only garbage. Why is main() not accessing the contents of the memory positions of the array? </p>
  
  <pre><code>void createlist(char* file, char **mylist) {
  
      FILE* stream;
      char name[40];
      int i = 0;
  
      stream = fopen(file, "r");      
  
      while (fgets(name, sizeof(name), stream) != NULL) { 
  
          mylist[i] = name;
          printf(mylist[i]);   // &lt;------------------------This works OK! 
          i++;
      }
      fclose(stream);
  }
  
  void main(int argc, char* argv[]) {
  
      char list[3000][40]; 
      int i = 0;
      resetchararray(list, 3000);
  
      createlist("file", list);   
  
      for (i = 0; i &lt; 3000; i++) {
          printf(list[i]);    // &lt;---------------------This writes garbage
      }
  }
  </code></pre>