Thread: Help a noob please !

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

    Help a noob please !

    // int num_rooms = 24;

    // there exist a struct of type room_t, do i have any typecasting that i have to do?

    // allocate an array for all of the room structs to be stored
    // store the pointer in the global room_array variable
    /*** YOUR CODE HERE ***/
    room_array = malloc(num_rooms * sizeof(room_t));

    is this right?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You need to first define the struct, then declare an instance of it. You can typecast it, or not, as you please.

    Then you'll create your array to hold your structs. That array can be a fixed size (static), or can be changed in size (dynamic), using malloc, etc.

    Do you want or need to make the array, dynamic? Is the room number of 24 going to be constant, throughout the program?

    Post up your attempt at this, in code or pseudo-code, and tell us what the end purpose of this program, is going to be.

  3. #3
    Stressed Student :(
    Join Date
    Feb 2008
    Location
    Berkeley, CA
    Posts
    73
    room_array is already a global variable in a file global.h and room_array is in globals.h
    i believe i have my syntax all wrong can u please help?
    i'm trying to set the room substructs to null

    sorry edit note:
    the num_rooms integer is taken from reading a file via fgets(buf, 256, levelfile) (which is the first integer in the file)


    okay, here's what i've got so far
    Code:
    /* We need this typedef because exit_t contains a room_t*,
     * but the compiler hasn't seen room_t yet. This works
     * because pointers are all the same size, and the
     * compiler doesn't need to know what *kind* of pointer a
     * room_t* is. */
    typedef struct room room_t;
    
    
    
        // allocate an array for all of the room structs to be stored
        // store the pointer in the global room_array variable
        /*** YOUR CODE HERE ***/
        room_array = (room_t*)malloc(num_rooms * sizeof(room_t));
    
        // Initialize room_array
        /*** YOUR CODE HERE ***/
        for (int i = 0; i < num_rooms; i++)
            {
                (room_array[i])->room_id = i;
                (room_array[i])->description = NULL;
                (room_array[i])->exit_t = NULL;
                (room_array[i])->puzzle_t = NULL;
                (room_array[i])->mob_t = NULL;
    
            }
    the problem i'm getting is that room_array[i]->room_id gives me an error. this is part 1 of my project and its suppose to simulate an array of rooms (room_t)
    Last edited by NoobieGecko; 02-15-2008 at 03:05 AM.

  4. #4
    Stressed Student :(
    Join Date
    Feb 2008
    Location
    Berkeley, CA
    Posts
    73
    Ive actually figured out the error yet i dont know how i have fixed it! i changed -> operators to . operators which remarkably showed me a new error which is incompatible argument values assigned to the substructs, can someone explain why?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Without knowing what the struct looks like, it's impossible for us to know what you SHOULD be assigning your struct.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ive actually figured out the error yet i dont know how i have fixed it! i changed -> operators to . operators which remarkably showed me a new error which is incompatible argument values assigned to the substructs, can someone explain why?
    room_array[i] is a room_t object, not a pointer to a room_t object. As such, you use normal member access notation, e.g., room_array[i].room_id, not pointer member access notation, e.g., room_array[i]->room_id.

    If you want to post an error message, copy and paste it, and state which line is indicated by the compiler if it does not correspond to your code.

    Better yet, write the smallest and simplest program that demonstrates the error.

    By the way, why does room_t have members named exit_t, puzzle_t and mob_t? The _t suffix hints that they are types, but they are actually member variables.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Stressed Student :(
    Join Date
    Feb 2008
    Location
    Berkeley, CA
    Posts
    73
    I'm sorry about the confusion, I'm still trying to set all the room_id, description, mob_t, puzzle_t to either null or false. Sorry, but ty for the replies here is what I have:

    this is level.h
    Code:
    #ifndef LEVEL_H
    #define LEVEL_H
    
    #include "monsters.h"
    #include "commands.h"
    #include <stdbool.h>
    
    typedef enum {
        NORTH,
        SOUTH,
        EAST,
        WEST,
        UP,
        DOWN,
    
        NUM_DIRECTIONS
    } direction;
    
    // maps direction enums to human readable direction strings
    extern char *direction_names[];
    
    // flips a direction enum (hint: enums start from 0 and increase)
    #define flip_direction(original) (original ^ 1)
    
    /* We need this typedef because exit_t contains a room_t*,
     * but the compiler hasn't seen room_t yet. This works
     * because pointers are all the same size, and the
     * compiler doesn't need to know what *kind* of pointer a
     * room_t* is. */
    typedef struct room room_t;
    
    // a struct representing an exit
    typedef struct {
        room_t *dest;
        bool locked;
    } exit_t;
    
    // don't worry about the below struct
    // the two functions which bring a puzzle to life!
    typedef struct {
        // prints a description of the puzzle
        cmd_fxn_t description_fxn;
        
        // lets the player attempt to solve the puzzle
        cmd_fxn_t interact_fxn;    
    } puzzle_t;
    
    // 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;
    };
    
    
    // loads a level config file and returns a pointer to the starting room
    // (FINISH IMPLEMENTING THIS FUNCTION IN level.c)
    room_t *load_level(char *filename);
    
    #endif
    and this is what i have worked on
    this is "level.c"
    Code:
    #include "level.h"
    #include "common.h"
    #include "util.h"
    
    // array of directio names. must be consistent with the order of the enum
    // in level.h
    char *direction_names[] = {
        "north",
        "south",
        "east",
        "west",
        "up",
        "down"
    };
    
    
    // loads a level from a config file and returns a pointer to the starting room
    room_t *load_level(char *filename) {
        char buf[2048];
        char *whitespace = " \t\n";
        FILE *levelfile = fopen(filename,"r");
    
        if(levelfile == NULL) {
            printf("Could not open &#37;s\n", filename);
            exit(1);
        }
    
        skip_characters(levelfile, whitespace);
        
        // get the total number of rooms
        fgets(buf,256,levelfile);
        num_rooms = atoi(buf);
    
        skip_characters(levelfile, whitespace);
    
        // allocate an array for all of the room structs to be stored
        // store the pointer in the global room_array variable
        /*** YOUR CODE HERE ***/
        room_array = (room_t *)malloc(num_rooms * sizeof(room_t));
    
        // Initialize room_array
        /*** YOUR CODE HERE ***/
        for (int i = 0; i < num_rooms; i++)
            {
                room_array[i].room_id = i;
                room_array[i].description = NULL;
                room_array[i].exits = (exit_t *)NULL;
                room_array[i].mob = (mob_t *)NULL;
    
            }
    
        skip_characters(levelfile, whitespace);
    
    	// one line at a time, read in room description strings and set 
    	// the appropriate field in each string's corresponding room struct
        while(fgets(buf, 256, levelfile), buf[0] != '\n') {
    		/*** YOUR CODE HERE ***/
        }
    
        skip_characters(levelfile, whitespace);
    
    	// hook up rooms so that exits point to each other appropriately.
    	// exits should also be set to locked or unlocked as necessary
        while(fgets(buf, 256, levelfile), buf[0] != '\n' && !feof(levelfile)) {
            char *words[32];
            tokenizer(buf, words, " \t\n");
            
    		assert(!strcmp("can-go", words[0]) || !strcmp("cant-go", words[0]));
            
    		direction dir;
            switch(words[1][0]) {
                case 'n':
                    dir = NORTH;
                    break;
                case 's':
                    dir = SOUTH;
                    break;
                case 'e':
                    dir = EAST;
                    break;
                case 'w':
                    dir = WEST;
                    break;
                case 'u':
                    dir = UP;
                    break;
                case 'd':
                    dir = DOWN;
                    break;
                default:
                    printf("%s isn't a direction\n", words[1]);
                    assert(false);
            }
    		
    		/*** YOUR CODE HERE ***/
        }
    
        return room_array; // equivalent to a pointer to the first element of the array
    }

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
                room_array[i].exits = (exit_t *)NULL;
    exits is an array, not a pointer, so you can't set it to NULL.

    And unless "mob_t" is a pointer type, you probably don't want to set that to NULL either.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Stressed Student :(
    Join Date
    Feb 2008
    Location
    Berkeley, CA
    Posts
    73
    Thanks mats! the thing is the directions i'm given is really confusing or i can't read well -- i found out the length of exits is six, i have still yet to figure out how to set it so that an exits can be NULL

    this is given in level.h

    Code:
    // a struct representing an exit
    typedef struct {
        room_t *dest;
        bool locked;
    } exit_t;
    and the directions say
    "Each exit_t that is not used (does not exist in the game) should keep its dest pointer set to NULL."

    so how do i set it to null? do i just go room_array[i].exits.dest* = NULL; ?

  10. #10
    Stressed Student :(
    Join Date
    Feb 2008
    Location
    Berkeley, CA
    Posts
    73
    I'm hopeless

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    so how do i set it to null? do i just go room_array[i].exits.dest* = NULL; ?
    More like:
    Code:
    room_array[i].exits.dest = NULL;
    But then exits is an array, so actually you would use a loop and set:
    Code:
    room_array[i].exits[j].dest = NULL;
    where j is the inner loop index.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Stressed Student :(
    Join Date
    Feb 2008
    Location
    Berkeley, CA
    Posts
    73
    Thanx!! i was going to do that too but if
    Code:
    room_t dest*;
    is a pointer of struct exit_t (member?)
    is there any way to set the entire pointer to the array exits to null?

    this is the error i got when i set the room_array[i].exits.dest = NULL;

    ~/proj1 > gmake
    gcc -std=c99 -g -Wall -c -o obj/level.o level.c
    level.c: In function `load_level':
    level.c:48: error: request for member `dest' in something not a structure or union
    gmake: *** [obj/level.o] Error 1

  13. #13
    Stressed Student :(
    Join Date
    Feb 2008
    Location
    Berkeley, CA
    Posts
    73
    Because the problem I'm also having is setting the boolean "locked" to false

    Code:
    // a struct representing an exit
    typedef struct {
        room_t *dest;
        bool locked;
    } exit_t;

  14. #14
    Stressed Student :(
    Join Date
    Feb 2008
    Location
    Berkeley, CA
    Posts
    73
    Oh, nevermind, actually, i have to thank u again for saving me hours of time, i think i've confused myself more with the second question, because:

    exit_t contains an N number of exits which is either "locked" or has a "dest"(destination) one of which i set to null and the other to false.


    Can i ask another question? what is false in C? i tried "FALSE" but it didn't seem to work

  15. #15
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by NoobieGecko View Post
    what is false in C? i tried "FALSE" but it didn't seem to work
    0 is used as false in C
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob Q: How to read a value of a byte in binary?
    By Hitsuin in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2009, 02:46 PM
  2. Noob printf question
    By lolguy in forum C Programming
    Replies: 3
    Last Post: 12-14-2008, 08:08 PM
  3. I'm a noob and I need help.
    By nifear4 in forum C Programming
    Replies: 17
    Last Post: 10-14-2008, 01:20 PM
  4. noob needs help!!!!:(
    By tykenfitz in forum C Programming
    Replies: 1
    Last Post: 07-10-2005, 08:49 AM
  5. noob: compiling error... plz help!
    By chosii in forum C Programming
    Replies: 2
    Last Post: 05-10-2002, 05:53 AM