Zeroing out member arrays in a Structure

This is a discussion on Zeroing out member arrays in a Structure within the C++ Programming forums, part of the General Programming Boards category; I am making a Black Jack card game and I am using a structure to hold the player cards and ...

  1. #1
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    Zeroing out member arrays in a Structure

    I am making a Black Jack card game and I am using a structure to hold the player cards and information however when I go to redeal a new hand i need to zero out the old hand otherwise it just keeps adding cards. I am trying to use memset to zero out just the particular member variable arrays but for some reason it still just keeps the same values from the previous hand.

    Code:
    struct Players
    {
    	char name[MAX_PLAYERS][80];
    	char *card[MAX_PLAYERS][MAX_HAND];	//card names
    	char *suit[MAX_PLAYERS][MAX_HAND];	//card suits
    	int value[MAX_PLAYERS][MAX_HAND];	//point value of cards
    	int index[MAX_PLAYERS][MAX_HAND];	//index number of cards in hand
    	int total_cards[MAX_PLAYERS];	//total number of cards in hand
    	int Aces[MAX_PLAYERS];  //number of Aces in hand
    	int Money[MAX_PLAYERS];  //total winnings 
    	int Bet[MAX_PLAYERS];  //current bet amount
    };
    
    code i am using to try to zero out the member arrays
    
    		memset(&players.Aces ,0,sizeof(players.Aces[MAX_PLAYERS] ));
    		memset(&players.Bet  ,0,sizeof(players.Bet[MAX_PLAYERS] ));
    		memset(&players.card  ,0,sizeof(players.card[MAX_PLAYERS][MAX_HAND]  ));
    		memset(&players.index  ,0,sizeof(players.index[MAX_PLAYERS][MAX_HAND]  ));
    		memset(&players.suit  ,0,sizeof(players.suit[MAX_PLAYERS][MAX_HAND]  ));
    		memset(&players.total_cards ,0,sizeof(players.total_cards[MAX_PLAYERS]));
    		memset(&players.value ,0,sizeof(players.value[MAX_PLAYERS][MAX_HAND] ));
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    put the following in your main() (or some place that knows what players and MAX_PLAYERS are:

    Code:
     printf("sizeof(players.Aces[MAX_PLAYERS]) = %d\n", 
          sizeof(players.Aces[MAX_PLAYERS]));
    This tells the number of bytes of memory that an int takes in your system. (Not very useful in your context.)


    Furthermore, instead of memset, just use loops:

    Code:
    int i;
    
    for (i = 0; i < MAXPLAYERS; i++) {
      players.Aces[i] = 0;
      players.Bet[i] = 0;
      /* you get the idea, I hope */
    }

    Simpler, easier to read and understand; guaranteed portable.

    Dave

  3. #3
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    I figured it out.

    Thanks for the info Dave. (is memset not portable?) I found I wasn't passing a pointer properly so the deck pointer was not incrementing it just started over and that is why they cards were not changing. Also I removed the [MAX_PLAYERS] from the memset since I was trying to get the size of the array not the size of the array element and it works fine now.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    536

    Re: I figured it out.

    Originally posted by manofsteel972
    (is memset not portable?)
    Yes it is part of the C Standard Library, and, therefore is portable.

    Oh, well, I'm just predjudiced against direct memory manipulation instead of commonly used control structures.

    Regards,

    Dave

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    To save some space, memset the whole structure in 1 step:
    Code:
    memset(&players, 0, sizeof(Players));
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Regarding accessing the structure member
    By kollurisrinu in forum C Programming
    Replies: 5
    Last Post: 06-18-2008, 05:51 AM
  2. Problem with arrays inside a structure
    By babu in forum C Programming
    Replies: 4
    Last Post: 07-12-2007, 10:35 AM
  3. Character arrays in a Structure
    By vsriharsha in forum C Programming
    Replies: 7
    Last Post: 07-11-2004, 06:36 PM
  4. structure member initialization
    By ivandn in forum C Programming
    Replies: 4
    Last Post: 10-27-2001, 02:27 PM
  5. structure member values
    By breed in forum C Programming
    Replies: 3
    Last Post: 10-25-2001, 07:20 AM

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