警告 - 预期为“struct node **"但参数类型为“struct node **"是什么意思? [英] What does the warning - expected ‘struct node **’ but argument is of type ‘struct node **’ mean?
问题描述
我从数组创建树的代码:
My code for tree creation from array:
#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 **
类型.是bug吗?
I am not able to understand what does this warning say? Both expected and actual are of type struct node **
. Is it a bug?
推荐答案
编辑后(这就是我们要求 [mcve] 的原因),很清楚问题所在.
After the edit (that's why we ask for a [mcve]), it is clear what the problem is.
在您的 struct
中,您引用了一个 struct 节点
.但是你没有定义那个类型,你只是为一个本身没有名字的结构定义了别名node
.
Inside your struct
, you reference a struct node
. But you do not define that type, you only define the alias node
for a struct which has no name by itself.
请注意,在 C 中,struct node
驻留在与变量或 typedef
ed 别名等正常"名称不同的命名空间中.
Note that in C struct node
resides in a different namespace than "normal" names like variables or typedef
ed aliases.
所以你必须添加一个标签:
So you have to add a tag:
typedef struct node {
struct node* left;
struct node* right;
int val;
} node;
这样你就有了一个 struct node
以及一个名为 node
的类型.
That way you have a struct node
as well as a type with name node
.
这篇关于警告 - 预期为“struct node **"但参数类型为“struct node **"是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!