0
votes
#include<stdio.h>
#include<stdlib.h>

typedef struct              //create a pointer structure
{
int data;
struct node *left;
struct node *right;
} node;

node *insert(node *root, int x);

//Insert Function
node *insert(node *root, int x)    //Line 53        
{    
    if(!root)
    {
        root = new_node(x);
        return root;
    }

    if (root->data > x)
    {
        root->left = insert(root->left, x);   //Line 63
    }

    else 
    {
        root->right = insert(root->right, x);  // Line 68
    }

    return root;
}

I get the following errors while compiling:

: In function ‘insert’:

:63:3: warning: passing argument 1 of ‘insert’ from incompatible pointer type [enabled by default]

:53:7: note: expected ‘struct node *’ but argument is of type ‘struct node *’

:63:14: warning: assignment from incompatible pointer type [enabled by default]

:68:3: warning: passing argument 1 of ‘insert’ from incompatible pointer type [enabled by default]

:53:7: note: expected ‘struct node *’ but argument is of type ‘struct node *’

:68:15: warning: assignment from incompatible pointer type [enabled by default]

  1. Why is the 1st argument in my insert function incompatible with what I am passing into it?
  2. How do you assign a pointer to a 'struct pointer'?
1

1 Answers

0
votes

Change the struct definition to

struct node             //create a pointer structure
{
    int data;
    struct node *left;
    struct node *right;
};

and the function prototype to

struct node *insert(struct node *root, int x);