Подтвердить что ты не робот

Ошибка: требуется преобразование в нескалярный тип

У меня возникла небольшая проблема с попыткой создания этой структуры. Вот код для структуры:

typedef struct stats {                  
    int strength;               
    int wisdom;                 
    int agility;                
} stats;

typedef struct inventory {
    int n_items;
    char **wepons;
    char **armor;
    char **potions;
    char **special;
} inventory;

typedef struct rooms {
    int n_monsters;
    int visited;
    struct rooms *nentry;
    struct rooms *sentry;
    struct rooms *wentry;
    struct rooms *eentry;
    struct monster *monsters;
} rooms;

typedef struct monster {
    int difficulty;
    char *name;
    char *type;
    int hp;
} monster;

typedef struct dungeon {
    char *name;
    int n_rooms;
    rooms *rm;
} dungeon;

typedef struct player {
    int maxhealth;
    int curhealth;
    int mana;
    char *class;
    char *condition;
    stats stats;
    rooms c_room;
} player;

typedef struct game_structure {
    player p1;
    dungeon d;
} game_structure;

И вот код, с которым у меня проблема:

dungeon d1 = (dungeon) malloc(sizeof(dungeon));

Это дает мне ошибку "error: преобразование в нескалярный тип, запрошенный" Может кто-нибудь помочь мне понять, почему это?

4b9b3361

Ответ 1

Вы не можете ничего отличить от типа структуры. Я предполагаю, что вы хотели написать:

dungeon *d1 = (dungeon *)malloc(sizeof(dungeon));

Но, пожалуйста, не бросайте возвращаемое значение malloc() в программу C.

dungeon *d1 = malloc(sizeof(dungeon));

Будет работать нормально и не будет скрывать от вас ошибки #include.

Ответ 2

malloc возвращает указатель, поэтому, вероятно, вы хотите следующее:

dungeon* d1 = malloc(sizeof(dungeon));

Вот что выглядит malloc:

void *malloc( size_t size );

Как вы можете видеть, он возвращает void*, однако вы не должны указывать возвращаемое значение.

Ответ 3

Память, назначенная malloc, должна храниться в указателе на объект, а не на самом объекте:

dungeon *d1 = malloc(sizeof(dungeon));