Problem with pointers to structs

This is a discussion on Problem with pointers to structs within the C Programming forums, part of the General Programming Boards category; Ok, I have been here before and forgot how to do this Basically I have a struct declared as: Code: ...

  1. #1
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218

    Problem with pointers to structs

    Ok, I have been here before and forgot how to do this

    Basically I have a struct declared as:
    Code:
    typedef struct
    {
        char name[20];
        Uint8 hand[5];
        Uint8 cards;
        Uint32 cash;
        Uint8 active;// If in round or not
        // Applies to bots only 
        Uint8 bluff; 
        Uint8 skill; 
        Uint8 memory;
    }player;
    In main I create an array of players and pass teh array to a function:
    Code:
    player *players[G_NUM_PLAY];    
    InitPlayers(players);
    I pass the players to the function, which compiles fine, but whenever I try and access any of the struct variables my prog gibbs out:
    Code:
    void InitPlayers(player **p)
    {
        // Set up player
        printf("Name yourself: ");
        GetStr(p[0]->name, 20);
        p[0]->cash = G_START_CASH;
        p[0]->bluff = 0;
    
        // Set up bots
        Uint8 i;
        for(i=1; i<G_NUM_PLAY; i++)
        {
            p[i]->cash = G_START_CASH;
            strcpy(p[i]->name, GenerateName());
            p[i]->bluff = rand() % 5;
        }
        
        // Determine who starts as the dealer
        G_DEALER = rand() % G_NUM_PLAY;
        
        // Print game start status
        printf("You are playing: ");
        for(i=1; i<G_NUM_PLAY-1; i++)
            printf("%s, ", p[i]->name);
        printf("and %s.\n", p[i]->name);
        printf("Each player starts with %u chips\n", p[0]->cash);
        printf("%s starts as the dealer\n", p[G_DEALER]->name);
        system("PAUSE");    
    }
    What am I doing wrong here?

    Thanks.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    First thing that comes to mind is... did you allocate space with malloc? Since you're creating an array of pointers...
    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.

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Oh.. Um.. No lol thanks elysia.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with file handling (pointers)
    By hmk in forum C Programming
    Replies: 5
    Last Post: 09-19-2008, 10:03 AM
  2. Problem with pointers
    By kotoko in forum C Programming
    Replies: 3
    Last Post: 06-12-2008, 05:17 AM
  3. problem using pointers
    By thestien in forum C++ Programming
    Replies: 3
    Last Post: 05-02-2008, 05:02 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. pointers and structs
    By alexir in forum C Programming
    Replies: 0
    Last Post: 10-20-2001, 01:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21