You know that moment when your trying to learn but you're missing some small detail and just can't figure it out? I'm having one of those and would be extremely grateful if it were over. Anyway here's my code:
// I'm trying to implement a load function for a trie, a dictionary in particular
// I'm getting a segfault when it arrives at strcpy, but when I check the value of 'word' there, it's NULL
bool load(const char *dictionary) {
FILE *file = fopen(dictionary, "r");
char *word = NULL;
char input[45] = { '\0' };
int size = sizeof(node);
while (fscanf(file, "%s", word) != EOF) {
printf("%s", word);
strcpy(input, word);
int cycle = 0;
node *next = &root;
while (input[cycle] != '\0') {
int position = toupper(input[cycle]) % 65;
if (position == 39) {
position = 26;
}
if (next->children[position] == NULL) {
next->children[position] = malloc(size);
next = next->children[position];
} else {
next = next->children[position];
}
}
*sizePointer = *sizePointer + 1;
next->wordHere = true;
}
fclose(file);
return true;
}