Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is my function not accepting a double pointer?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading