从类型'ZipperTree'分配类型'struct ZipperNode'时不兼容的类型

时间:2022-07-15 16:13:46

So, I'm getting this gcc compilation error when compiling:

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

zipper.c: In function ‘fillZipperInfo’: zipper.c:384:22: error: incompatible types when assigning to type ‘struct ZipperNode’ from type ‘ZipperTree’ forest[current++] = n;

zipper.c:在函数'fillZipperInfo'中:zipper.c:384:22:错误:从类型'ZipperTree'中分配类型'struct ZipperNode'时不兼容的类型[current ++] = n;

Here's the opaque type definition on the "zipper.h" file:

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

typedef struct ZipperNode *ZipperTree;

And here the ZipperNode definition on the "zipper.c" file:

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

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

And the surrounding section where the error is being called:

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

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!!
    }
}

And here's the ft type, FreqTable, just in case, which is also pre-defined on "zipper.h":

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

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

And I think this might be all I need to make the following question: what might be causing the error at the start of the post?

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

Thanks for your answers.

谢谢你的回答。

1 个解决方案

#1


By replacing the ZipperTree types with ZipperNode * is becomes easier to see what is going wrong

通过使用ZipperNode *替换ZipperTree类型变得更容易看出出了什么问题

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

forest[current++] is your lvalue which, when indexing, is going to be the value pointed to, a ZipperNode, not a ZipperNode * which is what your rvalue is.

forest [current ++]是你的左值,当索引时,它将是一个ZipperNode,而不是ZipperNode *,这是你的右值。

Compiling with clang gives a little more information but ultimately it is complaining about the same thing.

使用clang进行编译会提供更多信息,但最终它会抱怨同样的事情。

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

You are assigning a pointer to a non-pointer type. Try dereferencing n

您正在指定指向非指针类型的指针。尝试解除引用n

#1


By replacing the ZipperTree types with ZipperNode * is becomes easier to see what is going wrong

通过使用ZipperNode *替换ZipperTree类型变得更容易看出出了什么问题

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

forest[current++] is your lvalue which, when indexing, is going to be the value pointed to, a ZipperNode, not a ZipperNode * which is what your rvalue is.

forest [current ++]是你的左值,当索引时,它将是一个ZipperNode,而不是ZipperNode *,这是你的右值。

Compiling with clang gives a little more information but ultimately it is complaining about the same thing.

使用clang进行编译会提供更多信息,但最终它会抱怨同样的事情。

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

You are assigning a pointer to a non-pointer type. Try dereferencing n

您正在指定指向非指针类型的指针。尝试解除引用n