title: Storing each line of a text file into an array
body: <p>I am trying to save each line of a text file into an array.
  They way I am doing it and works fine so far is this :</p>
  
  <pre><code>char *lines[40];
  char line[50];
  int i = 0 ;
  char* eof ;
  while( (eof = fgets(line, 50, in)) != NULL )
  {
      lines[i] = strdup(eof); /*Fills the array with line of the txt file one by one*/
      i++;
  }
  </code></pre>
  
  <p>My text file has 40 lines , which I am accessing with a for loop</p>
  
  <pre><code>for( j = 0; j &lt;= 39 ; j++)
  { /*Do something to each line*/}.
  </code></pre>
  
  <p>So far so good. My problem is that i define the size of the array <strong>lines</strong>
  for the a text file that has 40 lines. I tried to count the lines and then define the size but I am getting <strong>segmentation fault</strong>.</p>
  
  <p>My approach:</p>
  
  <pre><code>int count=1 ; char c ;
  for (c = getc(in); c != EOF; c = getc(in))
      if (c == '\n') // Increment count if this character is newline
          count = count + 1;
  printf("\nNUMBER OF LINES = %d \n",count); 
  
  char* lines[count];
  </code></pre>
  
  <p>Any ideas ?</p>