Search code examples
cpointersstructincompatibletypeerror

Assignment from incompatible pointer type with structs


I have a function where I have the lines in scope.c, and is failing on the third line with an incompatible pointer type error.

struct scope* newScope = malloc(sizeof(struct scope));
newScope->symbols = createSymbolTable();
newScope->st = createSyntaxTree();

The struct scope in scope.h is defined as:

struct scope {
    char *id;
    struct symboltable* symbols;
    struct symboltree*  st;
    struct symboltable* strings;
};

And the prototype for the function createSyntaxTree() in syntax.h is

struct syntaxtree* createSyntaxTree();

I could understand having issues if I were dealing with typedefs, but this is pretty straightfoward, and the types on both sides are of type syntaxtree*.

How do I solve this frustrating error?


Solution

  • type of scope::st is struct symboltree*. The return type of createSyntaxTree() is struct syntaxtree*. They are different types. Hence,

    newScope->st = createSyntaxTree();
    

    is a problem.

    Perhaps you meant to use:

    struct syntaxtree*  st;
    

    instead of

    struct symboltree*  st;
    

    when defining struct scope.