0

I've been stuck on the load portion of Speller. Feels like I have a grasp on what I'm supposed to do but I keep getting use of undeclared identifier "head". Any guidance is appreciated. Am I headed in the right direction?

while (fscanf(file, "%s", word) != EOF)
{
    //allocate memory for new node
    node *new_node = malloc(sizeof(node));

    //increase word count
    counter++;

    //check for valid node
    if(new_node == NULL)
    {
        unload();
        return false;
    }

    strcpy(new_node ->word, word);

    int y = hash(word);

    if(hashtable[y] == NULL)
    {
        node *head = malloc(sizeof(node));

        if(head == NULL)
    {
        exit (1);
    }

        strcpy(head->word ,word);
        head->next = NULL;

        hashtable[y] = head;

    }else
    {
        node *node_link = malloc(sizeof(node));



        if(node_link == NULL)
    {
        exit (1);
    }

        strcpy(node_link->word ,word);


        node_link ->next = head;
        head = node_link;
    }

    free(new_node);
1

It's a scope issue. The code creates head inside the code block for an IF statement. Once that code block ends, the head variable ceases to exist. When the code then tries to use it later, the compiler interprets this as trying to use an undeclared variable.

If you want the head var to be used in a wider scope, it needs to be declared outside of any localized code block (bound by curly braces).

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .