C “实现二叉搜索树”;“从不兼容的指针类型返回”;

C “实现二叉搜索树”;“从不兼容的指针类型返回”;,c,pointers,binary-search-tree,C,Pointers,Binary Search Tree,我目前正在学习C语言,在实现二进制搜索树时遇到了一个问题 我已经声明了节点和树结构,但是编译器为以下四个函数提供了“不兼容指针类型”警告,这些函数返回或修改每个节点的左指针和右指针 我已经试过阅读其他类似的问题,但我不明白为什么,因为所有类型都应该是node* #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct{ int data; struct

我目前正在学习C语言,在实现二进制搜索树时遇到了一个问题

我已经声明了节点和树结构,但是编译器为以下四个函数提供了“不兼容指针类型”警告,这些函数返回或修改每个节点的左指针和右指针

我已经试过阅读其他类似的问题,但我不明白为什么,因为所有类型都应该是
node*

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

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

typedef struct{
    node *root;
    int size;
} tree;

node* getLeft(node *n){
    return n->left;
}

node* getRight(node *n){
    return n->right;
}

void setLeft(node *n, node *i){
    n->left=i;
}

void setRight(node *n, node *d){
    n->right=d;
}

int main() {
    return 0;
}

节点
typedef
声明不正确。它指的是不存在的结构节点。修复方法只是使用命名结构,而不是匿名结构(可能是您打算做的)


哇,这就解决了!非常感谢。那么,声明匿名结构是一种不好的做法,还是有某些条件我应该像这样注意?@leftright1匿名结构在语法上很好,但在结构是自引用时不能使用(就像在本例中一样)。但是请注意,有些人会认为typedefing结构是一种糟糕的做法,因为它隐藏了类型的真正本质。@leftright1现在您使用的是两种“不同”的类型,它们实际上是相同的;这并不代表一致的编码风格。请删除无用的
typedef
s,并一致使用
struct节点
/
struct树
ask.c: In function ‘getLeft’:
ask.c:16:5: warning: return from incompatible pointer type
     return n->left;
     ^
ask.c: In function ‘getRight’:
ask.c:20:5: warning: return from incompatible pointer type
     return n->right;
     ^
ask.c: In function ‘setLeft’:
ask.c:24:12: warning: assignment from incompatible pointer type
     n->left=i;
            ^
ask.c: In function ‘setRight’:
ask.c:28:13: warning: assignment from incompatible pointer type
     n->right=d;
             ^
typedef struct node {
    int data;
    struct node *left, *right;
} node;