warning - expected ‘struct node **’ but argument is of type ‘struct node **’ 是什么意思?
What does the warning - expected ‘struct node **’ but argument is of type ‘struct node **’ mean?
我从数组创建树的代码:
#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)
^
我不明白这个警告说了什么? expected 和 actual 都是 struct node **
类型。这是一个错误吗?
编辑后(这就是为什么我们要求[mcve]),很清楚问题是什么。
在您的 struct
中,您引用了一个 struct node
。但是您没有定义该类型,您只为本身没有名称的结构定义别名node
。
请注意,在 C 语言中,struct node
驻留在与 "normal" 变量或 typedef
ed 别名等名称不同的名称空间中。
所以你必须添加一个标签:
typedef struct node {
struct node* left;
struct node* right;
int val;
} node;
这样你就有了一个 struct node
以及一个名称为 node
.
的类型
我从数组创建树的代码:
#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)
^
我不明白这个警告说了什么? expected 和 actual 都是 struct node **
类型。这是一个错误吗?
编辑后(这就是为什么我们要求[mcve]),很清楚问题是什么。
在您的 struct
中,您引用了一个 struct node
。但是您没有定义该类型,您只为本身没有名称的结构定义别名node
。
请注意,在 C 语言中,struct node
驻留在与 "normal" 变量或 typedef
ed 别名等名称不同的名称空间中。
所以你必须添加一个标签:
typedef struct node {
struct node* left;
struct node* right;
int val;
} node;
这样你就有了一个 struct node
以及一个名称为 node
.