错误:转换到非标量型要求 [英] Error: Conversion to non-scalar type requested

查看:225
本文介绍了错误:转换到非标量型要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题试图对malloc这个结构。
这里是code的结构:

I'm having a small problem trying to malloc this struct. Here is the code for the structure:

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;

这是我遇到一个问题,code:

And here is the code I'm having a problem with:

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

这给我的错误错误:转换要求的非标型
有人能帮助我理解这是为什么?

It gives me the error "error: conversion to non-scalar type requested" Can someone help me understand why this is?

推荐答案

您可以不投任何一个结构类型。我你的意思写presume是:

You can't cast anything to a structure type. What I presume you meant to write is:

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

但请不要投的返回值的malloc() C程序。

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

将工作得很好,不会对你隐瞒的#include 的错误。

这篇关于错误:转换到非标量型要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆