Search code examples
cpointersstructincompatibletypeerror

incompatible types when assigning to type ‘struct ZipperNode’ from type ‘ZipperTree’


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

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

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

typedef struct ZipperNode *ZipperTree;

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

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":

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.


Solution

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

    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.

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

    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