加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

错误:转换为非标量类型请求

发布时间:2020-12-16 07:51:11 所属栏目:百科 来源:网络整理
导读:我有一个小问题,试图malloc这个结构. 这是结构的代码: 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;typed
我有一个小问题,试图malloc这个结构.
这是结构的代码:
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));

它给我错误“错误:转换为非标量类型请求”
有人能帮我理解为什么会这样吗?

解决方法

您不能将任何东西投射到结构类型.我想你想写的是:
dungeon *d1 = (dungeon *)malloc(sizeof(dungeon));

但是请不要在C程序中转换malloc()的返回值.

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

将工作很好,不会隐藏#include错误从你.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读