Q:

Segmentation Fault on fputs

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....)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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 > 0; i--) {     
      fputs(*(lines+i), output);            //segfault here
   }

}

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?

A:

Your problem is here:
lines = realloc(lines, (lineNumber) * 1000 * sizeof(char));

exactly as @ooga said.  There are at least three separate things wrong with it:

You are reallocating the memory block pointed to by recursive_back()'s local variable lines, and storing the new address (supposing that the reallocation succeeds) back into that local variable.  The new location is not necessarily the same as the old, but the only pointer to it is a local variable that goes out of scope at the end of recursive_back().  The caller's corresponding variable is not changed (including when the caller is recursive_back() itself), and therefore can no longer be relied upon to be a valid pointer after recursive_back() returns.
You allocate space using the wrong type.  lines has type char **, so the object it points to has type char *, but you are reserving space based on the size of char instead.
You are not reserving enough space, at least on the first call, when lineNumber is zero.  On that call, when the space requested is exactly zero bytes, the effect of the realloc() is to free the memory pointed to by lines.  On subsequent calls, the space allocated is always one line's worth less than you think you are allocating.

It looks like the realloc() is altogether unnecessary if you can rely on the input to have at most 1000 lines, so you should consider just removing it.  If you genuinely do need to be able to reallocate in a way that the caller will see, then the caller needs to pass a pointer to its variable, so that recursive_back() can modify it via that pointer.

