Sreeraj Chundayil
Sreeraj Chundayil

Reputation: 5859

What does the warning - expected ‘struct node **’ but argument is of type ‘struct node **’ mean?

My code for tree creation from array:

#include<stdio.h>
#include<malloc.h>
typedef struct
{
    struct node* left;
    struct node* right;
    int val;
}node;

void create_tree(node** root,int val)
{

    if(*root == NULL )
    {
        *root = (node*)malloc(sizeof(node));
        (*root)->val = val;
        (*root)->left = NULL;
        (*root)->right = NULL;
        return;
    }   
    if((*root)->val <= val )
        create_tree(&((*root)->left),val);
    else
        create_tree(&((*root)->right),val);

    return ;    
}



int main()
{
    node *root = (node*)malloc(sizeof(node));
    root->val = 10;
    root->left = NULL;
    root->right = NULL;
    int val[] = { 11,16,6,20,19,8,14,4,0,1,15,18,3,13,9,21,5,17,2,7,12};
    int i;
    for(i=0;i<22;i++)   
        create_tree(&root,val[i]);
    return 0;
}

warning I am getting:

tree.c:10:6: note: expected ‘struct node **’ but argument is of type ‘struct node **’
 void create_tree(node** root,int val)
      ^

I am not able to understand what does this warning say? Both expected and actual are of type struct node **. Is it a bug?

Upvotes: 0

Views: 8334

Answers (1)

too honest for this site
too honest for this site

Reputation: 12263

After the edit (that's why we ask for a [mcve]), it is clear what the problem is.

Inside your struct, you reference a struct node. But you do not define that type, you only define the alias node for a struct which has no name by itself.

Note that in C struct node resides in a different namespace than "normal" names like variables or typedefed aliases.

So you have to add a tag:

typedef struct node {
    struct node* left;
    struct node* right;
    int val;
} node;

That way you have a struct node as well as a type with name node.

Upvotes: 1

Related Questions