20

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;

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?

hlovdal
  • 26,565
  • 10
  • 94
  • 165
atb
  • 943
  • 4
  • 14
  • 30

3 Answers3

26

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

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

But please don't cast the return value of malloc() in a C program.

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

Will work just fine and won't hide #include bugs from you.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • 4
    what is the problem if you cast the return value of malloc()? – Pritesh Acharya Apr 23 '13 at 05:27
  • @PriteshAcharya, probably not much with modern compilers. That said, it's non-idiomatic. Read [this question and its answers](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) for plenty of detailed discussion. – Carl Norum Apr 23 '13 at 05:31
  • `struct student_simple { int rollno; char *name; };` What is the difference between `struct student_simple *s2 = malloc(sizeof(struct student_simple *)); struct student_simple *s3 = malloc(sizeof(struct student_simple ));` I am able to use both s2 and s3 without any problem but when I check the size in gdb gdb$ `p sizeof(struct student_simple)` gives 16 gdb$ `p sizeof(struct student_simple *)` gives 8 how does a malloc of 8 bytes store the student_simple structure.? – Pritesh Acharya Apr 23 '13 at 05:49
  • 1
    Undefined behaviour is undefined. Anything could happen, including the appearance of correct behaviour. Use the `s3` form, it's correct. You should probably post questions as questions, rather than as comments on unrelated questions, though. – Carl Norum Apr 23 '13 at 06:01
  • @PriteshAcharya you should allocate the actual size needed for the data inside the struct, not the size of the pointer (hence NO asterisk). The pointer is just a memory address and it usually occupies a couple of bytes (after all it's just one number). The data inside the structure has no virtual limit, can be terabytes for what matters. If your struct is very small like in this case, you may not notice the difference because the number of bytes you're allocating is enough. Try with a much much bigger struct and you'll see the problem. – mfloris Nov 24 '16 at 08:37
3

malloc returns a pointer, so probably what you want is the following:

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

Here is what malloc looks like:

void *malloc( size_t size );

As you can see it return void*, however you shouldn't cast the return value.

Community
  • 1
  • 1
Jesse Good
  • 50,901
  • 14
  • 124
  • 166
0

The memory assigned by malloc must be stored in a pointer to an object, not in the object itself:

dungeon *d1 = malloc(sizeof(dungeon));
Adam Liss
  • 47,594
  • 12
  • 108
  • 150