【问题标题】:incompatible types when assigning to type ‘struct ZipperNode’ from type ‘ZipperTree’从“ZipperTree”类型分配给“struct ZipperNode”类型时不兼容的类型
【发布时间】:2015-05-02 18:36:54
【问题描述】:

所以,我在编译时遇到了这个 gcc 编译错误:

zipper.c:在函数“fillZipperInfo”中: zipper.c:384:22:错误:从“ZipperTree”类型分配给“struct ZipperNode”类型时,类型不兼容 森林[当前++] = n;

这是“zipper.h”文件中的不透明类型定义:

typedef struct ZipperNode *ZipperTree;

这里是“zipper.c”文件中的 ZipperNode 定义:

struct ZipperNode {
    int count;
    ZipperTree left;
    ZipperTree right;
    Symbol symbol;
};

以及调用错误的周围部分:

ZipperTree forest = malloc(sizeof(ZipperTree) * ft->total);
int current = 0;
int i;
for(i = 0; i < ft->size; i++) {
    if(ft->symbols[i] > 0) {
        ZipperTree n = malloc(sizeof(ZipperTree));
        n->count = ft->symbols[i];
        n->symbol = i;
        forest[current++] = n; //here!!
    }
}

这里是 ft 类型,FreqTable,以防万一,它也在“zipper.h”上预定义:

struct FreqTable {
    int symLength;
    size_t size;
    Symbol* symbols;
    int total;
};

我认为这可能就是我提出以下问题所需要的全部内容:在帖子开头可能导致错误的原因是什么?

感谢您的回答。

【问题讨论】:

    标签: c pointers struct incompatibletypeerror


    【解决方案1】:

    通过将 ZipperTree 类型替换为 ZipperNode * 可以更容易地查看问题所在

    ZipperNode *forest = malloc(sizeof(ZipperNode *);
    ZipperNode *n = malloc(sizeof(ZipperNode *));
    forest[current++] = n; 
    

    forest[current++] 是您的左值,在索引时,它将是指向的值,ZipperNode,而不是您的右值的ZipperNode *

    使用 clang 编译提供了更多信息,但最终它抱怨的是同一件事。

    error: assigning to 'struct ZipperNode' from incompatible type 'ZipperTree'
      (aka 'struct ZipperNode *'); dereference with *
        forest[current++] = n; //here!!
                          ^ ~
                            *
    

    您正在将指针分配给非指针类型。尝试取消引用n

    【讨论】:

    • 我最终做了类似的事情:我将第一行替换为: [code]ZipperTree* forest = malloc(sizeof(ZipperTree) * ft->total);所以是的,几乎是一样的:P
    猜你喜欢
    • 2021-01-17
    • 2021-02-10
    • 2015-03-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-25
    • 1970-01-01
    • 2016-01-23
    • 2016-02-24
    相关资源
    最近更新 更多