Danny Varghese
Danny Varghese

Reputation: 13

cannot convert node** to node** error for this given code in c

I am getting "cannot convert node** to node** error" for the given code. Here both are node**. Error is getting while the function insertintoBST is compared to its declaration. node is a structure variable. Here, I have tried to implement insertion into a binary search tree.

#include<stdio.h>
#include<conio.h>

void inserttoBST(struct node**,int,int);

void main()
{
    int choice,i,j;

    struct node{
    int value;
    int key;
    struct node*left;
    struct node*right;
    };

    struct node*root=NULL;

    do{
        printf("\n1.insert\n2.delete\n3.view list\n4.search");
        scanf("%d",&choice);

        switch(choice)
        {
            case 1: printf("\nEnter the key and data :");
                scanf("%d%d",&i,&j);
                inserttoBST(&root,i,j);
                break;
            case 2: break;
            case 3:break;
            case 4:break;
            default:printf("\nInvalid Entry");
        }
   }while(choice!=10);
}


void inserttoBST(struct node**root,int keys,int val)
    {
    if(*root==NULL)
        {
        struct node*nodes=(struct node*)malloc(sizeof(struct node));
        nodes->key=key;
        nodes->val=val;
        nodes->left=nodes->right=NULL;
        *root=nodes;
        }
    else if((*root)->key<keys)
        inserttoBST((*root)->right,int keys,int val);
    else    inserttoBST((*root)->left,int keys,int val);
    }

Upvotes: 0

Views: 1238

Answers (1)

paxdiablo
paxdiablo

Reputation: 881153

Your first problem is that struct node is not declared at the first point where you use it.

You need to move the definition out of main to before the inserttoBST prototype.

That's the first of many problems that code has, you also need to look at:

  • getting rid of conio.h, it's not a standard header.
  • including stdlib since that's needed for malloc.
  • figuring out whether your key variable should be key or keys.
  • figuring out whether your value structure field should be val or value.
  • getting rid of the int type specifiers from your calls to inserttoBST.
  • passing the correct value as the first argument to inserttoBST, specifically something like &((*root)->leftOrRight).
  • use int main (void) for the main function, this is one of the two canonical variants specifically allowed for by the standard.

That's everything I had to do to get it to compile, whether that's enough to remove any logic errors I couldn't say.

But, once you've got it to compile, that'll be the next step you need to take.

Upvotes: 3

Related Questions