incompatible types when assigning to type ‘tipoNo’ {aka ‘struct tipoNo’} from type ‘tipoNo *’ {aka ‘struct tipoNo *’} arv = inserir(&arv, 5);

Asked

Viewed 573 times

1

I’m trying to implement the method of inserting a binary search tree. This is the struct I’m using

typedef struct tipoNo
{
  int chave;
  struct tipoNo *esq, *dir; //apontadores da esquerda e direita
} tipoNo;

and my method of insertion is this :

tipoNo* inserir(tipoNo *r, int chave)
{
  if (r != NULL)
  {
    if (chave > r->chave)
      r->dir = inserir(r->dir, chave);
    else
      r->esq = inserir(r->esq, chave);
  }
  else
  {
    r = (tipoNo *)malloc(sizeof(tipoNo));
    r->chave = chave;
    r->dir = NULL; r->esq = NULL;
  }

  return r;
}

When I call the insert method in main I get the error of incompatible types

"incompatible types when assigning to type +type typeNo'} from type ' typeNo *' {aka ːstruct typeNo *'} Arv = insert(&Arv, 5);"

, I know Arv needs to be a pointer to receive a pointer but I couldn’t do this because when I did this implementation creating Arv as *Arv in main, I got the same error

"expected ẓtypeNo *' {aka ẓstruct typeNo *'} but argument is of type Type No **' {aka +struct typeNo **'} typeNo* insert(typeNo *r, int key);"

  tipoNo arv;
  criar(&arv);

  arv = inserir(&arv, 5);

If I do not put Arv to receive the return of the insert method, it gives segfault and when I put, I have this error. I know I need Arv to receive the return of the input method so that the tree I’m working with has the 'entered' values but I can’t get Arv to receive it.

  • Welcome to Stackoverflow, if possible adjust the title of your question. Ask your question. Code snippets in the title are not well viewed.

  • in this command arv = inserir(&arv, 5); the variable arv is declared as tipoNo, but the function inserir is declared as tipoNo*...you cannot assign a pointer to a variable that is not a pointer

3 answers

2

Since your structure has members of its own type, you will need a forward declaration before its full definition, see only:

typedef struct estruturaNo tipoNo; // forward declaration

struct estruturaNo
{
  int chave;
  tipoNo *esq;
  tipoNo *dir;
};

0

You are assigning a pointer (return of the "insert" function) to the "Arv" variable, which is not a pointer.

Correction:

// tipoNo arv; // <------------- ERRO
tipoNo* arv = NULL;

// criar(&arv); // <------------ DESNECESSARIO

arv = inserir(&arv, 5);

0

I don’t particularly like working with feedback when I’m using binary tree, lists... I’ll give you a link to a teacher who helps me work with it: Linear Lists

To declare structures I like to use:

typedef struct st_tipoNo
{
    int chave;
    tipoNo *esq;
    tipoNo *dir;
} t_tipoNo;

And to declare:

t_tipoNo *arv;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.