I have a function that scans a file and returns the number of the lines along with the lines in a string array, my function looks like this :
int load_lines(char* _file, char** _array){
FILE *infile;
char line_buffer[BUFSIZ];
char line_number;
infile = fopen(_file, "r");
ine_number = 0;
while (fgets(line_buffer, sizeof(line_buffer), infile))
++line_number;
fclose(infile);
_array = malloc (line_number * sizeof(char*));
infile = fopen(_file, "r");
line_number = 0;
while (fgets(line_buffer, sizeof(line_buffer), infile)) {
_array[line_number] = malloc(strlen(line_buffer) + 1);
strcpy(_array[line_number], line_buffer);
//a printf on _array[line_number] works fine here
++line_number;
}
return line_number;
}
When I call it like this:
char** _array;
line_number = load_lines(inname, _array);
_array[0];
I get a segmentation fault since the array seems to be not allocated after the return of the function.