Every time I try to allocate memory for array of strings, my program fails with an error message "error reading characters of strings". What am I doing wrong? Is there any problem with my memory allocation?
char** file2array(char *filename)
{
FILE *fp = fopen(filename, "r");
int i;
int max_line_len = 20;
int num_lines = 6;
char** lines = (char **) malloc(sizeof(char*) * (num_lines + 1));
fseek(fp, 0, SEEK_END);
int chars_num = ftell(fp);
rewind(fp);
for (i = 1; i < (num_lines + 1); i++) {
lines[i] = (char *) malloc(max_line_len + 1);
fgets(lines[i], chars_num, fp);
}
fclose(fp);
return lines;
}
fgetsis wrong.