Thread: Stuck, arrays

  1. #1
    Stressed Student :(
    Join Date
    Feb 2008
    Location
    Berkeley, CA
    Posts
    73

    Stuck, arrays

    I thought that I did this the right way, but it turns out I can't allocate more than 1 size of an array. (I'm supposed to initialize a room_array data structure which is an array of structs).

    I have this syntax.
    Code:
        int size_room = sizeof(struct room);
        room_t **room_structs  = (room_t **)malloc(num_rooms * size_room);
    This is the header

    Code:
    // a struct containing all the information which represents a room
    struct room {
        int room_id;
        char *description;
        mob_t mob;
        exit_t exits[NUM_DIRECTIONS];
        puzzle_t puzzle;
    };
    this is a follow up from my last post, can anyone help me? I've been stuck since 7 pm

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you do not need to cast malloc in C
    dynamical array of structs is allocated as

    Code:
    struct Node* pNodes = malloc(numOfNodes * sizeof *pNodes);
    your pointer to pointer notation is not suitable for one-dimentional array
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Stressed Student :(
    Join Date
    Feb 2008
    Location
    Berkeley, CA
    Posts
    73
    So the array is Node, not pNodes? Am I right?
    Code:
    room_t room_structs* room  = malloc(num_rooms * (sizeof room));

  4. #4
    Stressed Student :(
    Join Date
    Feb 2008
    Location
    Berkeley, CA
    Posts
    73
    but i get this error though
    Code:
    gcc -std=c99 -g -Wall -c -o obj/level.o level.c
    level.c: In function `load_level':
    level.c:39: error: syntax error before '*' token

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    struct room_t room_structs* room  = malloc(num_rooms * (sizeof room));
    Should do it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by Elysia View Post
    Code:
    struct room_t room_structs* room  = malloc(num_rooms * (sizeof room));
    Should do it.
    Why wouldn't this be:

    Code:
    struct room * room_structs = malloc(num_rooms * sizeof(room)) ;
    room is the type of the structure. I don't see a room_t. And, can you reuse room for a variable name of struct room?

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    OK, to get rid of confusion, once more:
    Code:
    struct room* rooms = malloc(num_rooms * sizeof(room));
    Sometimes I just scan through not double-checking it's right.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    not sizeof room

    or (better)
    sizeof *rooms

    or (not so good, but also will work)
    sizeof (struct room)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I ran a test instead of guessing.

    Final word.
    Code:
    #include <stdio.h>
    
    // a struct containing all the information which represents a room
    struct room {
    	int room_id;
    	char *description;
    	int mob;
    	double exits[10];
    	char * puzzle;
    } myroom, *room_ptr ;
    
    
    int main (int argc, const char * argv[]) {
    
        printf("sizeof(myroom) = %d\n", sizeof(myroom) );            // Use this... 
        printf("sizeof(struct room) = %d\n", sizeof(struct room) );  // Or this. 
    
        printf("sizeof(room_ptr) = %d\n", sizeof(room_ptr) );    // But not this.. 
        //printf("sizeof(* myroom) = %d\n", sizeof(* myroom) );  // and this generates a compile error 
        //printf("sizeof(* room) = %d\n", sizeof(* room) );      // and so does this.
        return 0;
    }
    Mainframe assembler programmer by trade. C coder when I can.

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Todd Burch View Post
    I ran a test instead of guessing.
    It was not a guess

    You a missing

    sizeof (*room_ptr );

    How could you ran tests if you fail to read the simple line of code?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by vart View Post
    It was not a guess
    I think he was referring to Elysia rather than yourself.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by vart View Post
    It was not a guess

    You a missing

    sizeof (*room_ptr );

    How could you ran tests if you fail to read the simple line of code?
    A little testy today, aren't we?

    Every test shown above I ran. And yes, I missed this one. Thank you for pointing out my omission.
    Code:
    #include <stdio.h>
    
    // a struct containing all the information which represents a room
    struct room {
    	int room_id;
    	char *description;
    	int mob;
    	double exits[10];
    	char * puzzle;
    } myroom, *room_ptr ;
    
    
    int main (int argc, const char * argv[]) {
    
        printf("sizeof(room) = &#37;d\n", sizeof(myroom) );              // Use this... 
        printf("sizeof(struct room) = %d\n", sizeof(struct room) );  // Or this, 
        printf("sizeof(* room_ptr) = %d\n", sizeof(* room_ptr) );    // Or this.
    
        printf("sizeof(room_ptr) = %d\n", sizeof(room_ptr) );    // But not this.. 
        //printf("sizeof(* myroom) = %d\n", sizeof(* myroom) );  // and this generates a compile error 
        //printf("sizeof(* room) = %d\n", sizeof(* room) );    // and so does this.
        return 0;
    }
    Todd
    Last edited by Dino; 02-16-2008 at 02:16 PM.
    Mainframe assembler programmer by trade. C coder when I can.

  13. #13
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by iMalc View Post
    I think he was referring to Elysia rather than yourself.
    Actually, I was referring to myself.
    Mainframe assembler programmer by trade. C coder when I can.

  14. #14
    Stressed Student :(
    Join Date
    Feb 2008
    Location
    Berkeley, CA
    Posts
    73
    Thanks guys, you are all awesome!
    Anyway I tried this:

    Code:
    struct room* rooms = malloc(num_rooms * sizeof(struct room));
    
    int siz = sizeof(rooms)/sizeof(rooms[0]);
    printf("&#37;d", siz);
    // I'm trying to get how many rooms i can store inside rooms
    // Because I'm trying to initialize each sub struct for each room inside rooms(character_id, exits, puzzle)
    // To null
    // But I get a zero for siz
    // And a 4 for sizeof(rooms)
    // How do I call rooms[0], rooms[1], etc.?
    // if rooms[0] = is a room
    Last edited by NoobieGecko; 02-16-2008 at 02:25 PM.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by NoobieGecko View Post
    Thanks guys, you are all awesome!
    Anyway I tried this:

    Code:
    struct room* rooms = malloc(num_rooms * sizeof(struct room));
    
    int siz = sizeof(rooms)/sizeof(rooms[0]);
    printf("&#37;d", siz);
    // I'm trying to get how many rooms i can store inside rooms
    // Because I'm trying to initialize each sub struct for each room inside rooms(character_id, exits, puzzle)
    // To null
    // But I get a zero for siz
    // And a 4 for sizeof(rooms)
    // How do I call rooms[0], rooms[1], etc.?
    // if rooms[0] = is a room
    I would guess that since rooms is a pointer, sizeof(rooms)==4; and since we're doing integer arithmetic, 4/(some number larger than 4) gives zero. You don't need to know siz, you've already got it, it's num_rooms.

    Edit to add: If you're worried that maybe you didn't get all the memory you asked for, you should recall that malloc returns NULL (instead of a valid pointer) if the memory allocation didn't succeed. So if (rooms==NULL) you need to panic; otherwise, you have all the memory you asked for.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  2. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  3. Stuck again: Arrays
    By bliss in forum C++ Programming
    Replies: 2
    Last Post: 05-13-2005, 12:37 AM
  4. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 02:34 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM