C语言编译时警告双指针

标签 c pointers struct compiler-warnings double-pointer

在这里,我创建了一个名为目录的结构。它具有指向子级的双指针和指向父级的单指针。

    typedef struct {
        struct directory** children;
        struct directory* parent ; 
    }directory;

在这里,我创建了一个添加目录的函数。

    directory* add_dir (char* name , char* path , directory* parent) {

        directory* d = malloc (sizeof (directory)) ;
        memset ( d , 0 , sizeof(directory)) ;
        //WARNING by line below
        d->children = (directory**)malloc ( sizeof(directory*) * DIR_COUNT) ;
        memset ( d->children , 0 , sizeof(directory*) * DIR_COUNT) ;
        //WARNING by line below
        d->parent  = (directory*) parent ;
            //Wanrning by line below
            parent->children[parent->alloc_num_child] = (directory*) d ;
    }

我已经定义了一个名为 directory 的结构,它有指向子目录的双指针和指向父目录的单指针。当我编译这个时,我收到了警告。

Warnings :


warning: assignment from incompatible pointer type [enabled by default]
warning: assignment from incompatible pointer type [enabled by default]
warning: assignment from incompatible pointer type [enabled by default]

我不确定为什么会收到此警告?

最佳答案

仔细查看您的结构声明。

您声明了一个名为 typedef 的目录。您没有在任何地方声明结构目录。但是您在声明的未标记结构中使用“结构目录”。

当您编写“struct directory”时,编译器无法猜测您指的是名为“directory”的typedef。

我通常会写

typedef struct _directory {
    struct _directory** children;
    struct _directory* parent ; 
}directory;

关于C语言编译时警告双指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23048544/

相关文章:

c - 分配结构指针数组?

c - 在 C 中通过定界符函数实现拆分字符串

c - 通过函数传递结构

c - struct sscanf 无法正常工作?

c++ - std::sort 函数给出 "Bus error: 10"

C 函数参数

c++ - 在 C++ 中通过 UDP 发送缓冲图像

c - 如何在不引用 c 对象的情况下导入使用 FFI 的 haskell 模块?

python - 周期性高斯的实现

c++ - 使用对象指针时如何访问成员函数