Home > Software engineering >  Pass pointer of struct with recursive function
Pass pointer of struct with recursive function

Time:03-11

I'm just trying to pass my struct in recursive function, but in the function when I will pass it again it will be the argument of my struct. I could get the right value but I get the error "passing argument 1 of 'search' from incompatible pointer type [-Wincompatible-pointer-types]gcc "

typedef struct node_t
{
int keyVal;
struct node_t *leftNode;
struct node_t *rightNode;
struct node_t *parent;
}Node;

typedef struct tree_t
{
 struct node_t *root;
}List;



Node *search( List *temp, int value)
    {  
        Node *curr=temp->root;
       if (curr == NULL || curr->keyVal == value)
           return curr;
        if (curr->keyVal < value)


      return search(&(curr->leftNode), value); //here I'm getting a warning
                     ^^^ 
    return search(&(curr->leftNode), value); //the same here 
}                ^^^ 

CodePudding user response:

Node *search( List *temp, int value) This takes a List aka tree_t as the first argument.

In return search(&(curr->leftNode), value); the curr->leftNode is a Node aka node_t. This is different type and the compiler is correct to complain about incompatible pointer type.

A possible fix would be to change the function signature to Node * search( Node * curr, int value) and remove Node *curr=temp->root;. Do the first call initiating the search as search(x->root, xxx); with your List. The recursive calls would then be correct.

  • Related