0

I am using the trie structure for doing the pset5 speller. However, my program can't compile due to 'redefinition of node' in 'typedef struct node', and 'declaration shadows a variable in the global scope in 'void free_all(node* root)'. I am not sure of how to solve the problems.

// Implements a dictionary's functionality
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "dictionary.h"

// Definition of node
typedef struct node
{
    bool is_word;
    struct node *children[27];
}
node;

node *root;
node* currentnode;
int numberofwords;

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    int numberword;
    currentnode = root;
    for(int j=0; j<strlen(word); j++)
    {
        // Turn to number (case-insensitive)
        if(isalpha(word[j]))
        {
            if(isupper(word[j]))
            {
                numberword = word[j] - 'A';
            }
            if(islower(word[j]))
            {
                numberword = word[j] - 'a';
            }
        }
        if(word[j]=='\'')
        {
            numberword = 26;
        }
        //Go to corresponding element in children (NULL: mispelled; otherwise to next character)
        if(currentnode -> children[numberword] != NULL)
        {
            currentnode = currentnode -> children[numberword];

        }
        if(currentnode -> children[numberword] == NULL)
        {
            return false;
        }
        //End of word: Check is_word true
    }
    if(currentnode -> is_word == true)
    {
        return true;
    }
    else
    {
        return false;
    }
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    currentnode = root;
    //open file
    FILE* largedictionary = fopen(dictionary, "r");
    //Load words
    char dictionaryword [45];
    int number;
    while(fscanf(largedictionary, "%s", dictionaryword) != EOF)
    {
       for (int i = 0; i<strlen(dictionaryword); i++)
       {
           //Convert character to number
           if (isalpha(dictionaryword[i]))
           {
               number = dictionaryword[0]-'a';
           }
           if(dictionaryword[i]=='\'')
           {
               number = 26;
           }
           //Make nodes
           if(currentnode->children[number] == NULL)
           {
               node *new_node = malloc(sizeof(node));
               if(new_node == NULL)
               {
                   unload();
                   return false;
               }
               new_node = currentnode->children[number];
               currentnode = new_node;
           }
       }
       numberofwords++;
    }

    return false;
    fclose(largedictionary);
}

// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
    // TODO
    return numberofwords;
}

// Unloads dictionary from memory, returning true if successful else false
//Function to free all
void free_all(node* root)
{
    for (int m=0; m<27; m++)
    {
        if(!root)
        {
            return;
        }
        if(root->children[m]!=NULL)
        {
            free_all(root -> children[m]);
        }
        free(root);
    }
}
//Freeing
bool unload(void)
{
    free_all(root);
    return false;
}
1
  • Are you sure you have saved your code recently? Line 105 (that the compiler is complaining about) is a } ?
    – curiouskiwi
    Jun 16, 2018 at 2:40

1 Answer 1

0

The compiler is unhappy because root is defined as a global and then redefined as a local var in void free_all(node* root). Worse, it's defined as a parameter for the function.

If you wanted to use the global root, then this could have been defined as a function that takes no parameters. However, this is clearly not the case because it recursively calls itself.

To fix it, you need to use a local variable name for the parameter and inside the function other than root. If you really want to use the global root inside the function for anything, then use it. Otherwise, use the local parameter.

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 .