0

I'm trying to complete Pset5 using a trie. So far, I've written the code for load() and it seems to work (at least, my counter variable for the number of words goes to 2 when I try to load dictionaries/small). But I wanted to double-check load() is working correctly, by printing out the contents of the trie. I figure I'm going to have to do something very similar for check() anyway (i.e. traverse the contents of the trie and "get at" each of the valid words in turn). I've written a recursive function to print the words, and declared this in dictionary.h. However, with the declaration that I've used:

void print_trie(struct node *pTemp, char* word, int l);

I get the error "declaration of 'struct node' will not be visible outside of this function [-Werror,-Wvisibility]"

Blauelf pointed out an error I'd made - I'd tried to declare the function inside my load() function. So I've now stripped it out to near the top of my dictionary.c file. But I'm still getting the same error.

I'd appreciate your help - I admit my understanding of function declaration and prototyping is, well, shaky at best!

If you care to look at the actual print_trie function too, that would be great! Many thanks

// Declare node struct. Declared globally so that it is accessible by all the functions, as required.
typedef struct node
{
    bool is_word; // is_word is TRUE of the node represents the end of a word.
    struct node *pChildren[ALPHABET + 1]; // 26 letters in Roman alphabet, plus the '/'' character.
}
node;

// Declare pointer to root node. Done globally to avoid error "initializer element is not a compile-time constant"
node *pRoot = NULL;

 // Function to print the contents of a trie
void print_trie(struct node* pTemp, char* word, int l);
{
    int l = 0; // Level down the trie
    char *word; // This will be the word we want to print
    node *pTemp = pRoot;

    for(int i = 0, y = (ALPHABET + 1); i < y; i++) // Loop through every pointer in the node (26 letters, plus the apostrophe).
                                                   // i is the index of the array pChildren in each node.
    {
        if(!(pTemp == NULL)) // If pointer is not null, at least one word contains this letter
        {
            word[l] = i; // Store this the "lth" character of the word we want to print
            l++; // Move "down" a level in the trie
            pTemp = pTemp->pChildren[i]; // Traverse the trie to the next level
            if((pTemp->is_word) == true) // If the "is_word" field is true, then we've reached the end of a word
            {
                word[l]='\0'; // Mark the end of a word by adding the null terminator
                for(int a=0, b=strlen(word); a<b; a++) // Convert the string "word" back from 0-based index
                {
                    if(word[a]<ALPHABET) // Characters with index 0-25 are alphabetic...
                    {
                        word[a] = word[a] + ALPHA_SHIFT; // ...so are shifted back from 0-based index by adding ALPHA_SHIFT (value 97)
                    }
                    else
                    {
                        if(word[a]==ALPHABET)
                        {
                            word[a] = word[a] + APOS_SHIFT; // If the char has an index of 26, it represents an apostrophe...
                                                            // ...so is shifted back from 0-based index by adding APOS_SHIFT (value 39)
                        }
                        else
                        {
                            word[a] = word[a]; // If the char has an index that isnot 0-26, it has already been shifted back from...
                                               // ...a 0-based index in a previous iteration - so must be left alone!
                        }
                    }    
                }
                printf("%s\n", word); // Print the word

                if(!(pTemp->pChildren[i] = NULL)) // Check whether a valid word in the trie has children (e.g. "CAT", "CATCH") 
                {
                    print_trie(pTemp, word, l); // If there are children, we have a follow-on word, so we repeat the process
                }
            }
            else // If "is_word" field is false, we didn't find the end of a word, so we repeat the process at the current level of the trie
            {
                print_trie(pTemp, word, l);
            }
        }
    }
}

I've then prototyped (? is that the right phrase?) the function in dictionary.h as follows:

void print_trie(struct node* pTemp, char* word, int l);
3
  • Is that a function declaration inside a function? I don't think it works that way... Also, the { after that "function declaration" suggests you meant to put a function definition inside the function, which would not work for the same reasons. – Blauelf Mar 15 '17 at 12:21
  • Many thanks for the help @Blauelf. That makes sense! However, I've now taken the function declaration out and put it separately at the top of dictionary.c. I still get the same error /dictionary.h:63:24: error: declaration of 'struct node' will not be visible outside of this function [-Werror,-Wvisibility] void print_trie(struct node pTemp, char word, int l). dictionary.h line 63 reads void print_trie(struct node pTemp, char word, int l); Does anyone have any ideas what I should do next? Many thanks – Matt Friend Mar 16 '17 at 10:03
  • As I understand it, (i) I've prototpyed my function in dictionary.c; (ii) I've declared it [by which I mean, written the code that sets out the function] on its own in dictionary.c; and (iii) I then call the function inside another function - load() within dictionary.c. But there's clearly something wrong! – Matt Friend Mar 16 '17 at 10:07
0

It seems the function declaration was the problem - once I'd taken the declaration and definition of the print_trie() function outside the load() function, and carefully put it in the right place in the dictionary.c file (e.g. making sure I'd prototyped it before the load() function, which calls it, the programme compiled.

I also realised that I didn't need to declare it in dictionary.h - speller.c doesn't use print_trie(), so it doesn't need to look in dictionary.h for it.

Thanks Blauelf!

You must log in to answer this question.

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