0

I am working on pset5 load but it doesn't compile. Could you have a look at my code?

/**
 * Loads dictionary into memory.  Returns true if successful else false.
 */
bool load(const char* dictionary)
{
    FILE* dic = fopen(dictionary, "r");

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

    node* root;

    int index;
    int prev_index;

    int c = fgetc(dic);

    //check all the characters until end of the file   
    while(c != EOF)
    {    
        //check whether the first character of a word is letter or apostrophe
        if(c == !'\'')
        {
            index = tolower(c) - 'a';
            prev_index = tolower(c) - 'a';

            if(root == NULL)
            {
                node* root = calloc(1, sizeof(node));
                root -> children[index] = c;
            }
        }
        else
        {
            index = 27;
            prev_index = 27;

            if(root == NULL)
            {
                 root -> children[index] = c;
            }
        }
        //iterate through all of the rest letters/apostrophes in the word
        while(c != '\0')
        {

            int c = fgetc(dic);
            node* children = calloc(1, sizeof(node));
            if(c == !'\'')

            {
                index = tolower(c) - 'a';

                if(children[index] == NULL)
                {
                    children[prev_index] -> children[index] = c;
                    prev_index = index;
                    index++;
                }
            }
            else
            {
                index = 27;

                if(children[index] == NULL)
                {
                    prev_index = tolower(c - 1) - 'a';
                    children[prev_index] -> children[index] = c;
                    prev_index = 27;
                    index++;
                }
            }
        }

        is_word = true;

        prev_index = 0;
        index = 0; 

        c = fgetc(dic); 
    }
return true;
}

When I try to make it compile it gives four major errors:

dictionary.c:59:41: error: incompatible integer to pointer conversion assigning to 'struct node *' from 'int' [-Werror,-Wint-conversion] root -> children[index] = c;

dictionary.c:83:36: error: invalid operands to binary expression ('node' (aka 'struct node') and 'void *') if(children[index] == NULL)

dictionary.c:97:42: error: member reference type 'node' (aka 'struct node') is not a pointer; maybe you meant to use '.'? children[prev_index] -> children[index] = c;

dictionary.c:104:9: error: use of undeclared identifier 'is_word' is_word = true; (I have declared it at the beginning when I declared struct, didn't I?)

What are the reasons behind the errors? Did I misunderstand how to use pointers? In what way?

Thank you in advance for your help!

0

1 Answer 1

0

1) "dictionary.c:59:41: error: incompatible integer to pointer conversion assigning to 'struct node *' from 'int' [-Werror,-Wint-conversion] root -> children[index] = c;"

children[index] is a pointer to node. You're trying to assign a char (which is the same as an int for all practical purposes) to it. Not too sure why you want to store the char anyway. You don't store anything in a trie

2) "dictionary.c:83:36: error: invalid operands to binary expression ('node' (aka 'struct node') and 'void *') if(children[index] == NULL)"

Not too sure about the error, but it could be related to the fact that you have declared a node pointer called "children" inside the while loop

node* children = calloc(1, sizeof(node));

This pointer is just a pointer, it's not an array, so that could cause the issue when you try to use children[index]. Same thing probably happens with the next error.

3) "dictionary.c:104:9: error: use of undeclared identifier 'is_word' is_word = true; (I have declared it at the beginning when I decleared struct, didn't I?)"

No. You didn't. is_word is a struct member, you can't use it in isolation. You need to use it as part of the node (or pointer to the node).

Also: You're typedeffing and declaring the struct inside the load function. What scope will that struct have?

1
  • Thank you, I needed a little time but finally realized how I misunderstood struct in this case. Thanks for your help!
    – Ervin
    Nov 12, 2015 at 8:46

You must log in to answer this question.

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