title: Do i need to use 2D arrays for an array of strings in C?
body: <p>I want my program to read <code>N</code> words from a text file and save them in an array. My question is, do i need a <code>2D Array</code> e.g: <code>char **wordList</code> or is the <code>1D Array</code> in the example below sufficient? The output is correct except from the last <code>string</code> which as you can see is weird. Also, am i allocating sufficient memory for the array and why does the last output string come out wrong?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  
  void populateWordsArray(int);
  
  FILE *file;
  char *word;
  char **wordList;
  
  /*
  * Function populateWordsArray: reads N words from
  * the given file and creates an array with them.
  * Has one argument: int N - the number of words to read.
  */
  void populateWordsArray(int N) {
      int i = 0;
      while(!feof(file) &amp;&amp; i &lt; N) {
      fscanf(file,"%s",&amp;word[i]);
      printf("%s\n",&amp;word[i]);
      i++;
      }
  }
  
  int main(int argc,char *argv[]) { // argv[1] = op argv[2] = name
      int N = 0;
  
      file = fopen(argv[2],"r");
  
      if(file == (FILE *) NULL) { // check if the file opened successfully
          fprintf(stderr,"Cannot open file\n");
      }
  
      fscanf(file,"%d",&amp;N); // get the N number
      word = malloc(N * sizeof(char));
  
      populateWordsArray(N);
      // write a switch method for the various ops
      // call the appropriate function for each operation
  
      free(word);
      fclose(file);
      return 0;
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>this
  is
  a
  test!
  with
  files.
  new
  line,
  here.
  ere.
  </code></pre>
  
  <p>text file content:</p>
  
  <pre><code>10 this is a test! with files.
  new line, here.
  </code></pre>