title: Segmentation Fault on fputs
body: <p>I am pretty new to C and memory allocation in general. Basically what I am trying to do is copy the contents of an input file of unknown size and reverse it's contents using recursion. I feel that I am very close, but I keep getting a segmentation fault when I try to put in the contents of what I presume to be the reversed contents of the file (I presume because I think I am doing it right....)</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  int recursive_back(char **lines, int lineNumber, FILE *input) {
  
     char *input_line = malloc(sizeof(char) * 1000);
     lines = realloc(lines, (lineNumber) * 1000 * sizeof(char));
     if(fgets(input_line, 201, input) == NULL) {
        *(lines + lineNumber) = input_line;
        return 1;
     }  
     else {
        printf("%d\n", lineNumber);
        return  (1+recursive_back(lines, ++lineNumber, input));
  
     }
  
  }
  
  void backward (FILE *input, FILE *output, int debugflag )   {
     int i;
     char **lines;                            //store lines in here
     lines = malloc(1000 * sizeof(char *) ); //1000 lines
  
     if(lines == NULL) {         //if malloc failed
        fprintf(stderr, "malloc of lines failed\n");
        exit(1);
     }
  
     int finalLineCount, lineCount;
     finalLineCount = recursive_back(lines, 0, input);
  
     printf("test %d\n", finalLineCount);
  
     for(i = finalLineCount; i &gt; 0; i--) {     
        fputs(*(lines+i), output);            //segfault here
     }
  
  }
  </code></pre>
  
  <p>I am using a simple input file to test the code. My input file is 6 lines long that says "This is a test input file". The actual input files are being opened in another function and passed over to the backward function. I have verified that the other functions in my program work since I have been playing around with different options. These two functions are the only functions that I am having trouble with. What am I doing wrong?</p>