c-gcc-error:取消引用指向不完整类型的指针



我有一组相当复杂的嵌套结构/联合,如图所示:

typedef enum {
    expr_BooleanExpr,
    expr_ArithmeticExpr
} expr_type;
typedef union {
    struct BooleanExpr *_bool;
    struct ArithmeticExpr *_arith;
} u_expr;
typedef struct {
    expr_type type;
    u_expr *expr;
} Expression;
typedef struct {
    Expression *lhs;
    char *op;
    Expression *rhs;
} BooleanExpr;
typedef struct {
    Expression *lhs;
    char *op;
    Expression *rhs;
} ArithmeticExpr;

gcc很乐意为我创建一个Expression结构,该结构的并集字段中包含BoolExpression值,如下所示:

Expression *BooleanExpr_init(Expression *lhs, char *op, Expression *rhs) {
    BooleanExpr *_bool = safe_alloc(sizeof(BooleanExpr));
    _bool->lhs = lhs;
    _bool->op = op;
    _bool->rhs = rhs;
    Expression *the_exp = safe_alloc(sizeof(Expression));
    the_exp->type = expr_BooleanExpr;
    the_exp->expr->_bool = _bool;
    return the_exp;
}

尽管它给出了一个警告:不兼容指针类型的赋值[默认启用]用于行:the_exp->expr->_bool = _bool;

然而,当访问诸如lhsrhs之类的内部表达式时,使用诸如之类的表达式

an_expr->expr->_bool->rhs

其中an_expr是以前创建的Expression结构,我得到了这篇文章标题中指定的错误。

我读到的很多内容都说,这是由于使用了->运算符,而.运算符是必需的。然而,这是不合适的,因为所有内容都是指针,所以需要隐式取消引用->运算符。

有什么想法吗?

您正在混合typedef标识符和struct作用域标识符。这行不通。做一些类似的事情

typedef struct  BooleanExpr BooleanExpr;

在您所有的struct声明之前,并且仅将这些声明作为

struct BooleanExpr { ...

没有CCD_ 11。

在您的代码中,您从未定义过struct BooleanExp,只定义了一个匿名的struct,您将其别名为标识符BooleanExp

最新更新