[Solved] I have a segmentation fault and am unsure about what is wrong with my code


The problem is obvious now you’ve said on which line the code crashes. Consider these lines…

    char *word = NULL;
    int i = 0; //index
    FILE *dictionaryTextFile;
    dictionaryTextFile = fopen(dictionary, "r");

    //scan for word
    while(fscanf(dictionaryTextFile, "%s", word) != EOF)

You’ve got 2 problems there. Firstly, you don’t check that the call to fopen worked. You should always check that the value returned is not NULL.

Secondly, and the cause of the crash, is that word is still NULL – you don’t allocate any space to hold a string in it. You might as well declare it the same as you declare it inside node so replace

    char *word = NULL;

with

    char word[LENGTH+1];

Speaking of node and to save you coming back with another crash later, you should always make sure you initialise all attributes of a struct. In this case new_node->next should be set to NULL as otherwise you’ll come to check it later in your for loop (which looks fine BTW) and it might appear to point to a node, but it’s pointing at some random place in memory and the code will crash.

1

solved I have a segmentation fault and am unsure about what is wrong with my code