I'm reading text from a file into an array of strings, when I run the program like this everything works fine, but if I comment out the lines "char letter = 'A'" and the printf two lines below it, there is a seg fault. Why is this? That was just to letter the lines, I don't see how this could result in a seg fault.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]){
FILE *file = fopen("file", "r");
if(file == NULL){
puts("File not found");
return 0;
}
char string[100], **array;
int idx = 0;
while(idx < 100 && fgets(string, 100, file) != NULL){
array[idx] = (char*) malloc(strlen(string));
string[strlen(string) - 1] = '\0';
strcpy(array[idx++], string);
}
char letter = 'A';
for(idx = 0; idx < 5; idx++){
printf("%c. ", letter++);
puts(array[idx]);
}
fclose(file);
return 0;
}