簡體   English   中英

C取消引用指向不完整類型的指針

[英]C dereferencing pointer to incomplete type

我閱讀了許多有關此問題的答案,但在這里看不到問題。

在dish.h中,我有:

typedef struct Dish_t* Dish;

並在dish.c中:

struct Dish_t {
 DishKind kind;
 Taste taste;
};

但是當我在dish.c中寫道:

Dish newDish = malloc(sizeof(*newDish));

我收到取消引用錯誤。 當我將Dish_t更改為Dish ,它起作用了! 為什么?

你說:

當我將Dish_t更改為Dish時,它起作用了! 為什么?

但是您的代碼實際上使用Dish 它不能與普通Dish_t ,因為這不是有效的類型名稱。 它必須是struct Dish_tDish

我建議不要在typedef中包含星號,因為它會增加更多的混亂機會。

你必須做

struct Dish_t newDish=malloc(sizeof(struct Dish_t));
Dish = &newDish;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM