I'm attempting to make a hangman game in C. I encountered this segmentation fault at the first line of this code block.
while (fgets(buf, LEN, fp)) {
int i = 0;
char buf[LEN];
FILE *fp = fopen("dictionary.txt", "r");
struct diction_t *dictionary = malloc(sizeof(struct diction_t));
assert(dictionary);
dictionary->nval = INIT;
dictionary->max = INIT;
dictionary->words = NULL;
/* uses doubling reallocation system */
while (fgets(buf, LEN, fp)) {
if (dictionary->words == NULL) {
dictionary->words = malloc(sizeof(char *));
assert(dictionary->words);
} else if (dictionary->nval > dictionary->max) {
dictionary->words = realloc(dictionary->words, GROW * dictionary->max *sizeof(char *));
assert(dictionary->words);
dictionary->max = GROW * dictionary->max;
}
dictionary->words[i] = malloc(sizeof(char) * LEN);
assert(dictionary->words[i]);
strncpy(dictionary->words[i], strtok(buf, "\n"), LEN);
i++;
dictionary->nval++;
}
dictionary->nval--;
fclose(fp);
return dictionary;
}
Never encountered this before so I honestly do not know where to look. Any help is appreciated!