从与结构不兼容的指针类型赋值

Assignment from incompatible pointer type with structs

我有一个函数,其中包含 scope.c 中的行,但在第三行失败并出现不兼容的指针类型错误。

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

scope.h中的结构域定义为:

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

syntax.h 中函数 createSyntaxTree() 的原型是

struct syntaxtree* createSyntaxTree();

如果我处理 typedef,我可以理解有问题,但这非常简单,两边的类型都是 syntaxtree* 类型。

如何解决这个令人沮丧的错误?

scope::st 的类型是 struct symboltree*createSyntaxTree() 的 return 类型是 struct syntaxtree*。它们是不同的类型。因此,

newScope->st = createSyntaxTree();

是个问题。

也许你打算使用:

struct syntaxtree*  st;

而不是

struct symboltree*  st;

定义时struct scope.