Thread: structures with pointers to structures

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    114

    Question structures with pointers to structures

    In my game I need a few different structures, I need one for each monster (says its location and state), one for each type of monster (says what image and things to use) and then one for each different animation (says how many frames, and what row its on). I thought i could maybe have a pointer to the monster type from the monster, and a pointer to the current animation from the monter type.

    Code:
    struct monster 
    {
        int x,y;
        enum Estate state; 
        monster_type *type;
    }
    
    struct monster_type
    {
        char *image;
    }
    
    monster monsters[100];
    monster_types[10];
    Then would i do "monsters[i].type = &monster_types[3]" to assign it? and then how would i use it all? For instance say if i wanted to find out what image the monster used. Could i do somthing like "monster.type.image" to get its image?

    Thanks, kzar

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Pointers to structures commonly use the "arrow" operator. Like so:
    Code:
    struct foo instance, *ptrto;
    
    /* accessing directly uses the "dot" operator: */
    instance.somemember = somevalue;
    
    /* assigning a pointer the address of an instance: */
    ptrto = &instance;
    
    /* accessing the member via a pointer to a structure uses the "arrow": */
    ptrto->somemember = somevalue;
    Now arrays are just like any other array:
    Code:
    struct foo array[SIZE];
    struct foo *ptrto;
    
    /* accessing directly again uses the "dot" operator: */
    array[ SOMEELEMENT ].somemember = somevalue;
    
    /* assigning a pointer to one of the array elements: */
    ptrto = &array[ SOMEELEMENT ];
    
    /* accessing via a pointer again uses the "arrow": */
    ptrto->somemember = somevalue;
    That what you were looking for?

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Then would i do "monsters[i].type = &monster_types[3]" to assign it?
    That's one way, sure.

    >Could i do somthing like "monster.type.image" to get its image?
    No, because type is a pointer to monster_type. Using your first example, you could retrieve the image like so:
    Code:
    monster[i].type->image
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    114
    thanks guys, I think i have it figured out in my brain now . I wasnt even sure if there was a way to get the image like that and i always just used the .'s unless the compiler gave an error and then i used ->'s lol i never understood why it needed them!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Understanding linked lists, structures, and pointers
    By yougene in forum C Programming
    Replies: 5
    Last Post: 07-13-2011, 08:13 PM
  2. Copying pointers in structures instead of the structure data?
    By Sparrowhawk in forum C++ Programming
    Replies: 7
    Last Post: 02-23-2009, 06:04 PM
  3. returning pointers to structures
    By Giant in forum C++ Programming
    Replies: 2
    Last Post: 06-20-2005, 08:40 AM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Help with pointers and members of structures
    By klawton in forum C Programming
    Replies: 2
    Last Post: 04-19-2002, 12:34 PM