0

I'm getting an pesky error when trying to compile my code.

error: use of undeclared identifier 'hashtable' first = hashtable[i];

My code, per below, is an attemp to implement the check function from pset4 "speller". I have called "hashtable[i]" on other places before and I thought I had set it correctly.

This is where I initialize the hashtable (in main) and use the function (load) successfully

// Initialize hashtable
node *hashtable[N];
for (int i = 0; i < N; i++)
{
    hashtable[i] = NULL;
} 

This is the code that generates the error

// Returns true if word is in dictionary else false
bool check(const char *str)
{
    int i = hash(str); // Getting the where in the hashtable the word is
    node * first = NULL; // setting up a node
    first = hashtable[i]; // pointing the node to the beginning of the linked list where the word is

    for (int j = 0; j < 99999; j++) // just so it iterates till the end of the linkedlist, is supposed to stop way earlier
    {
        if (first == NULL) // checkin to see if reached the end of the linked list
        {
            return false;
        }
        else if (first->word == str) // comparing the word uploaded to the word
        {
            return true;
        }
        first = first->next; // iterating to the next item on the linkedlist
    }
}
0

It depends on where you declared hashtable[]. If you created it inside of load, or inside of any other function, it will disappear when that function completes and will no longer exist when check starts.

It has to be declared as a global var, outside of any function.

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

1
  • To follow on to what Cliff said, in the distribution code you are given, the hashtable is declared globally and then initialized in load. You shouldn't move that declaration. Also, you shouldn't have a main function in dictionary.c. Here's the distribution code for reference: cdn.cs50.net/2018/fall/psets/4/speller/hashtable/speller/…
    – curiouskiwi
    Mar 28 '19 at 5:17

You must log in to answer this question.

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