Home > Net >  Why is my function not accepting a double pointer?
Why is my function not accepting a double pointer?

Time:04-18

I have a hash table declared as follows:

node* table[26];

And below is the function supposed to free that hash table:

bool unload(void)
{
    // TODO
    for (int i = 0; i < 26; i  )
    {
        free_table(&table[i]);
        if (table[i] != NULL)
        {
            return false;
        }
    }
    return true;
}


void free_table(node** hash)
{
    if (*hash == NULL)
    {
        return;
    }
    free_table((*hash) -> next);
    free(*hash);
    *hash = NULL;
}

The problem is whenever I try to compile, I get the following error:

error: incompatible pointer types passing 'node **' (aka 'struct node **') to parameter of type 'node *' (aka 'struct node *'); remove & [-Werror,-Wincompatible-pointer-types]
free_table(&table[i]);
           ^~~~~~~~~

What am I doing wrong?

CodePudding user response:

free_table is recursively calling free_table again in line 4 of the definition of free_table as

free_table((*hash) -> next);

Also, did you declare free_table as

free_table(node ** hash);

(in a header file or otherwise) before defining it here? I suspect that you might have made a mistake in the function declaration, which resulted in the compilation error.

I am guessing that the type of the struct member next is node *, which means that the type of (*hash) -> next is also node *. Changing that line to the following should work, i.e. take the address of (*hash) -> next.

free_table(&((*hash) -> next));
  • Related