我从数组创建树的代码:
#include<stdio.h>
#include<malloc.h>
typedef struct
{
struct node* left;
struct node* right;
int val;
}node;
void create_tree(node** root,int val)
{
if(*root == NULL )
{
*root = (node*)malloc(sizeof(node));
(*root)->val = val;
(*root)->left = NULL;
(*root)->right = NULL;
return;
}
if((*root)->val <= val )
create_tree(&((*root)->left),val);
else
create_tree(&((*root)->right),val);
return ;
}
int main()
{
node *root = (node*)malloc(sizeof(node));
root->val = 10;
root->left = NULL;
root->right = NULL;
int val[] = { 11,16,6,20,19,8,14,4,0,1,15,18,3,13,9,21,5,17,2,7,12};
int i;
for(i=0;i<22;i++)
create_tree(&root,val[i]);
return 0;
}
警告我得到了:
tree.c:10:6: note: expected ‘struct node **’ but argument is of type ‘struct node **’
void create_tree(node** root,int val)
^
我无法理解这个警告说的是什么?预期和实际都是struct node **
类型。这是一个错误吗?
答案 0 :(得分:1)
编辑之后(这就是我们要求[mcve]的原因),很清楚问题是什么。
在struct
内,您引用了struct node
。但是你没有定义那种类型,你只为一个没有名字的结构定义了别名node
。
请注意,在C struct node
中,其名称空间与“普通”名称(如变量或typedef
ed别名)不同。
所以你必须添加一个标签:
typedef struct node {
struct node* left;
struct node* right;
int val;
} node;
这样您就拥有struct node
以及名称为node
的类型。