【问题标题】:What does the warning - expected ‘struct node **’ but argument is of type ‘struct node **’ mean?警告 - 预期的“结构节点**”但参数类型为“结构节点**”是什么意思?
【发布时间】:2016-02-06 20:12:19
【问题描述】:

我从数组创建树的代码:

#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吗?

【问题讨论】:

标签: c pointers tree


【解决方案1】:

编辑后(这就是我们要求 [mcve] 的原因),问题出在哪里。

在您的struct 中,您引用了struct node。但是您没有定义该类型,您只为一个本身没有名称的结构定义别名node

请注意,在 C 中,struct node 与变量或 typedefed 别名等“普通”名称位于不同的命名空间中。

所以你必须添加一个标签:

typedef struct node {
    struct node* left;
    struct node* right;
    int val;
} node;

这样你就有一个struct node 以及一个名为node 的类型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-20
    • 2015-12-07
    • 1970-01-01
    • 2020-11-21
    • 2020-07-17
    • 1970-01-01
    • 2015-03-17
    • 2016-08-08
    相关资源
    最近更新 更多