Thread: Accessing Structures Inside Structures

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    10

    Accessing Structures Inside Structures

    Hello,

    As you can see below, the program will hold the account data in one structure and then the character data in another structure. Eventually, 'player_data' will contain more structures to other parts of the program (but that's not the case). For some reason I cannot access the 'player_data' structure through the 'account_data' structure.

    This is what my current code looks like (for testing purposes).
    Code:
    int main(int argc, char *argv[])
    {
        acc_data[0].player[0]->class_id = 10;
        
        printf("class id: %d\n", acc_data[0].player[0]->class_id);
      
        system("PAUSE");
        return 0;
    }
    And here's the structures.

    Code:
    struct account_data
    {
        char user[NAME_LENGTH];
        char pass[NAME_LENGTH];
    
        struct player_data *player[MAX_CHARACTERS];
    
        char *buffer;
        char *incbuffer;
    
        short char_id;
        short char_num;
        short ingame;
    
        long attack_timer;
        long data_timer;
        long data_bytes;
        long data_packets;
        long party_player;
    
        short in_party;
        short target_type;
        short target;
        short casted_spell;
        short party_starter;
        short getting_map;
    } acc_data[100];
    
    struct player_data
    {
        char name[NAME_LENGTH];
    
        short sex_id;
        short sprite_id;
        short class_id;
        short access_id;
    
        short level;
        long exp;
        short pk;
        short guild;
    
        long hp;
        long mp;
        long sp;
    
        short str;
        short def;
        short spd;
        short mgc;
        short pnts;
    
        short slot_armor;
        short slot_weapon;
        short slot_helmet;
        short slot_shield;
    
        int map_id;
        short coord_x;
        short coord_y;
        short dir;
    };
    What am I doing wrong, because every time I use printf, the program crashes? I want to constantly add, change, and remove data in certain situations.

    EDIT: And is there a way to shorten a structure check using the array requested?
    Last edited by Mellowz; 01-13-2008 at 03:33 AM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    your player is just array of pointers

    - or you should allocate the array using malloc
    - or change it to array of structs
    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. accessing an array of structures
    By creative in forum C Programming
    Replies: 4
    Last Post: 12-27-2007, 12:52 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Replies: 2
    Last Post: 01-26-2003, 04:37 AM
  4. Accessing structures contained in structures
    By enigmaatj in forum C Programming
    Replies: 1
    Last Post: 04-18-2002, 08:53 AM
  5. Accessing a Specific Text Line Inside CEditView :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 04-14-2002, 08:12 PM