0

I'm not sure what exactly is wrong with my code. I ran it through Valgrind, which says "Looks like you're trying to use a variable that might not have a value? Take a closer look at line 145 of dictionary.c" specifically referring to this line of my code in the Check section:

while(strcmp((*cursor).word, new_array_for_textword) != 0) 

--- see my attached code for more info. I don't get why it is wrong, as the logical steps seem fine to me. Appreciate it if anyone could let me know what's wrong, thanks so much.

bool check(const char *word)
{
    //calculate length of the text word
    int wordlength = strlen(word);

    //allocate space for the new array in CHECK
    char new_array_for_textword[wordlength + 1];

    //copy word from text to new array so that we can make them all lower case
    for (int i = 0; i < wordlength; i++)
    {
        new_array_for_textword[i] = tolower(word[i]);
    }

    //in the array_for_textword you need to add \0
    new_array_for_textword[wordlength + 1] = '\0';

    //hash the word
    int hashnumber = hash(new_array_for_textword);

    //check the hash tables are not empty
    if(hashtable[hashnumber] == NULL)
    {
        return false;
    }

    node *cursor = hashtable[hashnumber];

    //compare both strings, as long as they are not the same, the node will proceed, until it hits the same strings
    while(strcmp((*cursor).word, new_array_for_textword) != 0)
    {
        cursor = (*cursor).next;
        if(cursor == NULL)
        {
            return false;
        }
    }

    return true;
}
2

Here's one problem:

//in the array_for_textword you need to add \0
new_array_for_textword[wordlength + 1] = '\0';

Remember, if an array is allocated with x+1 elements, the first element is 0, not 1, and the last element is x, not x+1.

There may be other issues. Often, a problem that manifests itself in check can be traced back to issues in load.

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

2
  • Oh thanks so much Cliff! This has solved the problem and the code has passed Check50! Silly me, I spent hours trying to figure out what's wrong and turns out it was just me forgetting that the first element of the array isn't 1 ! – Nic Moe Nov 11 '19 at 22:24
  • Great. And I bet you never make that mistake again! ;-) – Cliff B Nov 11 '19 at 22:52

You must log in to answer this question.

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