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


我试图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));

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


13009
2018-03-05 03:34


起源



答案:


您不能将任何内容转换为结构类型。我认为你打算写的是:

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

但请不要投出返回值 malloc() 在C程序中。

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

工作得很好,不会隐藏 #include 来自你的错误。


12
2018-03-05 03:37



如果你转换malloc()的返回值有什么问题? - Pritesh Acharya
@PriteshAcharya,可能与现代编译器不太相关。那就是说,这是非惯用的。读 这个问题及其答案 进行大量详细讨论。 - Carl Norum
struct student_simple { int rollno; char *name; };  有什么区别 struct student_simple *s2 = malloc(sizeof(struct student_simple *)); struct student_simple *s3 = malloc(sizeof(struct student_simple )); 我可以同时使用s2和s3,但是当我检查gdb gdb $中的大小时 p sizeof(struct student_simple) 给出16 gdb $ p sizeof(struct student_simple *) 给出8 8字节的malloc如何存储student_simple结构。 - Pritesh Acharya
未定义的行为未定义。任何事情都可能发生,包括正确行为的出现。使用 s3 形式,这是正确的。您应该将问题作为问题发布,而不是作为对不相关问题的评论。 - Carl Norum
@PriteshAcharya你应该在struct中分配数据所需的实际大小,而不是指针的大小(因此没有星号)。指针只是一个内存地址,它通常占用几个字节(毕竟它只是一个数字)。结构内部的数据没有虚拟限制,重要的是太字节数。如果您的结构非常小,就像在这种情况下一样,您可能不会注意到差异,因为您分配的字节数就足够了。尝试使用更大的结构,你会看到问题。 - mfloris


答案:


您不能将任何内容转换为结构类型。我认为你打算写的是:

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

但请不要投出返回值 malloc() 在C程序中。

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

工作得很好,不会隐藏 #include 来自你的错误。


12
2018-03-05 03:37



如果你转换malloc()的返回值有什么问题? - Pritesh Acharya
@PriteshAcharya,可能与现代编译器不太相关。那就是说,这是非惯用的。读 这个问题及其答案 进行大量详细讨论。 - Carl Norum
struct student_simple { int rollno; char *name; };  有什么区别 struct student_simple *s2 = malloc(sizeof(struct student_simple *)); struct student_simple *s3 = malloc(sizeof(struct student_simple )); 我可以同时使用s2和s3,但是当我检查gdb gdb $中的大小时 p sizeof(struct student_simple) 给出16 gdb $ p sizeof(struct student_simple *) 给出8 8字节的malloc如何存储student_simple结构。 - Pritesh Acharya
未定义的行为未定义。任何事情都可能发生,包括正确行为的出现。使用 s3 形式,这是正确的。您应该将问题作为问题发布,而不是作为对不相关问题的评论。 - Carl Norum
@PriteshAcharya你应该在struct中分配数据所需的实际大小,而不是指针的大小(因此没有星号)。指针只是一个内存地址,它通常占用几个字节(毕竟它只是一个数字)。结构内部的数据没有虚拟限制,重要的是太字节数。如果您的结构非常小,就像在这种情况下一样,您可能不会注意到差异,因为您分配的字节数就足够了。尝试使用更大的结构,你会看到问题。 - mfloris


malloc 返回一个指针,所以可能你想要的是以下内容:

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

这是malloc的样子:

void *malloc( size_t size );

正如你所看到的那样 void*不过你 不应该转换返回值


2
2018-03-05 03:38





分配的内存 malloc 必须存储在指向对象的指针中,而不是存储在对象本身中:

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

0
2018-03-05 03:37