Thread: Structs

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    68

    Structs

    I am currently using a struct to store players name, betting information for a simple Black Jack game.

    Code:
    struct player {
    	char name[80];
    	char *hand[10];
    	int wins;
    	int draw; 
    	int lost;
    	float money;
    	float currentbet;
    };
    Defined for 4 players
    Code:
    struct player players[4];
    Now the players all get 1000 assigned to the money, however when they get to 0 in the money I have this figured out, however I wish to delete them so e.g If its player 2
    players[1] is deleted.

    Now my questions are

    1) How do i delete this would i assign a pointer to players[1] and then use free?
    2) When i delete the player what is going to happen to the above indexes would I require to copy them all into a temp array and then copy them back to the smaller array structure?

    Sorry if this is confusing for anyone reading, I am quite confused on how to accomplish this.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    i don't think you can delete memory that you did not create.......

    my suggestion is to look up "malloc" for dynamic memory creation and then "free" to delete it (it might not be "free", i forget, it's been a while since i've done c)
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Look up linked lists and/or binary trees

    Basically you'd store the players in a list adding them as more come in and deleting them as they leave.

    And yes its malloc to allocate and free to deallocate.

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    68
    I was afraid Id require a linked list makes things a lot harder, oh well i better read up on it.

  5. #5
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    one thing you could do if you dont want to use a linked list is have a bool checking if it is used or not, something along these lines

    Code:
    struct player {
    	char name[80];
    	char *hand[10];
    	int wins;
    	int draw; 
    	int lost;
    	float money;
    	float currentbet;
    
            BOOL used;
    };
    
    struct player players[4];
    then when checking an item you could do something alone the lines of..

    Code:
    for(i = 0; i <=4; i++)
    {
    	if(players[i].used == TRUE)
    	{
    		//do whats needed
    	}
    }
    and when you want to delete an item set the bool to false.
    Good Help Source all round
    Good help for win programmers
    you will probably find something in here
    this thing also helps

    if you have never tried any of the above then maybe you should, they help alot

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    BOOL is not a c type. I believe its a macro in <windows.h> if i remeber correctly.
    Woop?

  7. #7
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    sorry my bad, your right, im just use to using windows.h alot, instead of using a BOOL you could just use an integer
    Code:
    int used;
    and use 1 and 0 for true and false, that will also work
    Good Help Source all round
    Good help for win programmers
    you will probably find something in here
    this thing also helps

    if you have never tried any of the above then maybe you should, they help alot

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Or use C99 and _Bool.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 08:30 AM
  2. Multidimentional structs + memcpy() == FAIL
    By Viper187 in forum C Programming
    Replies: 8
    Last Post: 06-18-2008, 02:46 AM
  3. packed structs
    By moi in forum C Programming
    Replies: 4
    Last Post: 08-20-2002, 01:46 PM
  4. ArrayLists + Inner Structs
    By ginoitalo in forum C# Programming
    Replies: 5
    Last Post: 05-09-2002, 05:09 AM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM