0

Here is my code. I am trying to insert in binary search tree , with address to pointers when I am getting bigger data node i am going left and vice versa but

Getting Error: C:\Users\huf\Documents\tree.c|26|error: request for member `right' in something not a structure or union|

#include <stdio.h>

struct node
{
    struct node*left;
    int data;
    struct node *right;
};

void maketree(struct node **root1, int data1)
{
    if((*root1) == NULL)
    {
        (*root1) = (struct node *)malloc(sizeof(struct node));

        (*root1)->data = data1;
        (*root1)->left = NULL;
        (*root1)->right = NULL;
        //printf("%d %d",(*root1)->data,data1);
    }

   else if(data1 > ((*root1)->data))
    {
        printf("%d ", (*root1)->data);

        maketree((*root1)->right,data1);
    }

    else if(data1 < (*root1)->data)
    {
        maketree((*root1)->left,data1);
        printf("%d ", (*root1)->data);
    }

}

int main()
{

struct node * root = NULL;
//int data;
//data = 5;
maketree(&root,12);
maketree(&root,5);
maketree(&root,9);
maketree(&root,8);
maketree(&root,16);
maketree(&root,10);

return 0;
}

Why am i getting incompatible pointer type.

Ke7in
  • 917
  • 2
  • 7
  • 16

2 Answers2

2

Ths problem is in line maketree((*root1)->right,data1);. There must be struct node **root1 (see parameters of your function maketree()), but actual parameter is struct node *.

Try to put this line: maketree(&(*root1)->right,data1); (and the same for 'left' few lines lower).

Ilya
  • 4,583
  • 4
  • 26
  • 51
-2

The problem is here:

(*root1)->data = data1;

the type of root1 is void **, so the type of (*root1) is just void *, which does not point at a structure that has a data member.

You need to actually use the struct node type more, you seem hell-bent on using void * as much as possible.

Also, don't cast the return value of malloc() in C.

Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606