[英]C dereferencing pointer to incomplete type
I read many answers about this questions, and I can't see the problem here. 我阅读了许多有关此问题的答案,但在这里看不到问题。
In dish.h I have: 在dish.h中,我有:
typedef struct Dish_t* Dish;
and in dish.c: 并在dish.c中:
struct Dish_t {
DishKind kind;
Taste taste;
};
but when I write in dish.c: 但是当我在dish.c中写道:
Dish newDish = malloc(sizeof(*newDish));
I get the dereferencing error. 我收到取消引用错误。 When I change
Dish_t
to Dish
, it works! 当我将
Dish_t
更改为Dish
,它起作用了! Why? 为什么?
You say: 你说:
When I change Dish_t to Dish, it works!
当我将Dish_t更改为Dish时,它起作用了! Why?
为什么?
But your code actually uses Dish
. 但是您的代码实际上使用
Dish
。 It won't work with plain Dish_t
, since that's not a valid type name. 它不能与普通
Dish_t
,因为这不是有效的类型名称。 It must be either struct Dish_t
or Dish
. 它必须是
struct Dish_t
或Dish
。
I would recommend against including the asterisk in the typedef
since it adds even more chances of confusion. 我建议不要在
typedef
中包含星号,因为它会增加更多的混乱机会。
You have to do 你必须做
struct Dish_t newDish=malloc(sizeof(struct Dish_t));
Dish = &newDish;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.