Thread: I'm not really sure what to call this one

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    Ohio
    Posts
    37

    [RESOLVED] I'm not really sure what to call this one

    Okay I have a data structure called SPRITE. Then I have another data structure called PLANE. The SPRITE structure holds all the data that doesn't pertain to the actual bitmaps that make up the sprite and the PLANE structure holds an array of bitmaps that make up the sprite and a reference to the SPRITE structure. So it looks like this:

    Code:
    typedef struct SPRITE{
        SPRITE_STATE current_state;
        int x;
        int y;
        int width;
        int height;
        int xspeed;
        int yspeed;
        int xcount;
        int ycount;
        int xdelay;
        int ydelay;
        int curframe;
        int maxframe;
        int animdir;
        int framecount;
        int framedelay;
        int alive;
    }SPRITE;
     
    typedef struct PLANE{
        BITMAP *flying[3];
        SPRITE *sprite;
    }PLANE;
    Now my question is how do I set/get a value from the SPRITE structure through the PLANE structure?

    If that sounds confusing please let me know and I'll try to be more specific and thanks in advance for any help provided.
    Last edited by xmltorrent; 01-14-2007 at 01:05 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Like
    myplane->sprint.alive = false;

    ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Location
    Ohio
    Posts
    37
    hmm See that's what I thought too.

    In my main.c file, I created a reference to a PLANE structure like so:

    Code:
    PLANE *plane;
    (sorry for the lack of better naming)

    Then I tried doing this:

    Code:
    plane->sprite->x = 5;
    The program compiled fine but when I went to run it, it crashed on me.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well of course.

    For each variable with a * in it, you need to make sure it is allocated before you try and dereference it.

    Code:
    myplane = malloc( sizeof *myplane );
    myplane->sprite = malloc( sizeof *myplane->sprite );
    plane->sprite->x = 5;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Location
    Ohio
    Posts
    37
    Ah good call there. lol Thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. minix system call pls help for project
    By porvas in forum Linux Programming
    Replies: 2
    Last Post: 06-14-2009, 02:40 AM
  2. Error C2664 - Trying to call an external Dll
    By jamez05 in forum C++ Programming
    Replies: 3
    Last Post: 08-08-2006, 06:07 AM
  3. Class won't call
    By Aalmaron in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2006, 04:57 PM
  4. Iterative Tree Traversal using a stack
    By BigDaddyDrew in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2003, 05:44 PM
  5. call by reference and a call by value
    By IceCold in forum C Programming
    Replies: 4
    Last Post: 09-08-2001, 05:06 PM