Frage

can anyone help me understand these notes and warnings in my code? Specially the part where it says expected 'struct node**' but argument is of type 'struct node *' ". Also, on the recursive call:

tree_rebuild((*sroot)->esq, pOrd, emOrd, start, emOrd_index - 1);

is it correct to use (*sroot)->esq?

submission.c: In function ‘tree_rebuild’:
submission.c:42:3: warning: passing argument 1 of ‘tree_rebuild’ from incompatible pointer type [enabled by default]
  tree_rebuild((*sroot)->esq, pOrd, emOrd, start, emOrd_index - 1);
  ^
submission.c:11:5: note: expected ‘struct node **’ but argument is of type ‘struct node *’
int tree_rebuild(arv *sroot, char *pOrd, char *emOrd, int start, int end){
    ^
submission.c:43:3: warning: passing argument 1 of ‘tree_rebuild’ from incompatible pointer type [enabled by default]
  tree_rebuild((*sroot)->dir, pOrd, emOrd, emOrd_index +1, end);
  ^
submission.c:11:5: note: expected ‘struct node **’ but argument is of type ‘struct node *’
int tree_rebuild(arv *sroot, char *pOrd, char *emOrd, int start, int end){
    ^

submission.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct node{
  char info;
  struct node *dir;
  struct node *esq;
}Tree, *arv;

int tree_rebuild(arv *sroot, char *pOrd, char *emOrd, int start, int end){//l11

  static int preOrd_index = 0;

  *sroot = (Tree*)malloc(sizeof(Tree));

  (*sroot)->esq = NULL;
  (*sroot)->dir = NULL;
  printf("ok\n");

  if(start > end){
    return 0;
  }

  if(start == end){
    (*sroot)->info = emOrd[start];
    return 1;
  }

  int j = start;
  int emOrd_index;

  while(j < end){
    j++;
    if(pOrd[preOrd_index] == emOrd[j]){
      emOrd_index = j;
      break;
    }
  }

  preOrd_index++;
  tree_rebuild((*sroot)->esq, pOrd, emOrd, start, emOrd_index - 1); //l42
  tree_rebuild((*sroot)->dir, pOrd, emOrd, emOrd_index +1, end);    //l43

  return 0;
}

int main(){
  char sPre[100];
  char sEm[100];
  arv root;
  int n, tam;

  scanf("%d", &n);

  while(n){
    scanf("%d", &tam);
    scanf("%s", sPre);
    scanf("%s", sEm);
    tree_rebuild(&root, sPre, sEm, 0, tam-1);

    n--;
  }

  return 0;
}
War es hilfreich?

Lösung

Your recursive calls

tree_rebuild((*sroot)->esq, pOrd, emOrd, start, emOrd_index - 1); //l42
tree_rebuild((*sroot)->dir, pOrd, emOrd, emOrd_index +1, end);    //l43

Are passing a value of type struct node * from your type definition

typedef struct node{
  char info;
  struct node *dir;
  struct node *esq;
}Tree, *arv;

tree_rebuild is expecting a parameter of type arv * where arv * resolves to struct node **. The following will fix the compile error. It looks like your code is setup to handle the double pointer properly. You are just not passing the address of the struct member, and instead passing the value of the struct member.

tree_rebuild(&((*sroot)->esq), pOrd, emOrd, start, emOrd_index - 1); //l42
tree_rebuild(&((*sroot)->dir), pOrd, emOrd, emOrd_index +1, end);    //l43
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top