Reputation: 1282
I have a rather convoluted set of nested structs/unions as shown:
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 is happy for me to create an Expression struct containing a BoolExpression value in its union field as shown:
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;
}
although it gives a warning: assignment from incompatible pointer type [enabled by default] for the line: the_exp->expr->_bool = _bool;
However, when accessing the inner expressions such as lhs
and rhs
, with an expression like
an_expr->expr->_bool->rhs
where an_expr
is a previously created Expression struct, I get the error specified in the title of this post.
Much of what I've read says that this results from the use of the ->
operator where the .
operator is required. However this is not appropriate since everything is a pointer, so the implicit dereference of the ->
operator is required.
Any ideas?
Upvotes: 2
Views: 19120
Reputation: 78923
You are mixing typedef
identifiers and struct
scope identifiers. This can't work. Do something like
typedef struct BooleanExpr BooleanExpr;
before all your struct
declarations and have these only as
struct BooleanExpr { ...
without the typedef
.
In your code you never defined struct BooleanExp
but only an anonymous struct
that you alias to the identifier BooleanExp
.
Upvotes: 2