Thread: Accessing Structure Members

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    19

    Accessing Structure Members

    I am currently working on a client/server poker program. This file I have pulled separate for the time being while I play around with it and try to get it to work. This code is made to form the deck of cards, shuffle and print a hand. What I need to do however is use these cards later for comparison, etc. for the actual poker game. Is there a way to access individual cards here so that I may do that? Or is there a way to store individual structure elements into an array? I have been modifying this code now for hours trying to figure it out, unfortunately something is slipping by me I guess. Thanks for any help anyone can provide.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    typedef struct card { 
    	const char *face; 
    	const char *suit; 
    }Card; 
    
    /* typedef struct card Card; */ 
    
    typedef struct {
    	Card myCards[52];
    	unsigned int topCard;
    } Deck;
    
    void fillDeck( Card * const, const char*[], const char*[] ); 
    void shuffle( Card * const ); 
    
    void initDeck (Deck * const initMe, const char *face[], const char * suit[]);
    void Deal(Deck * const dealMe); 
    
    char const * getFace (Deck const * const getMyFace);
    char const * getSuit (Deck const * const getMySuit);
    
    int main() 
    { 
    	const char *face[] = { "Ace", "Two", "Three", "Four", 
    		"Five", "Six", "Seven", "Eight", 
    		"Nine", "Ten", "Jack", "Queen", "King"}; 
    	const char *suit[] = {"Diamonds", "Hearts", "Clubs", "Spades" }; 
    	
    	
    	Deck myDeck;
    	
    	srand(time(NULL ) ); 
    	
    	initDeck (&myDeck, face, suit);
    	Deal(&myDeck); 
    	
    	return 0; 
    } 
    
    void initDeck (Deck * const initMe, const char * face[], const char * suit[])
    {
    	initMe -> topCard = 0;
    	
    	fillDeck (initMe -> myCards, face, suit);
    	shuffle (initMe -> myCards);
    	
    	return;
    }
    
    void fillDeck( Card * const wDeck, const char * wFace[], const char * wSuit[]) 
    { 
    	int i; 
    	
    	for( i = 0; i <= 51; i++) { 
    		wDeck[ i ].face = wFace[ i % 13 ]; 
    		wDeck[ i ].suit = wSuit[ i / 13 ]; 
    	} 
    } 
    
    void shuffle( Card * const wDeck ) 
    { 
    	int i, j; 
    	Card temp; 
    	
    	for ( i = 0; i<= 51; i++ ) { 
    		j = rand() % 52; 
    		temp =wDeck[ i ]; 
    		wDeck[ i ] = wDeck[ j ]; 
    		wDeck[ j ] = temp; 
    	} 
    } 
    
    // Takes the top card off the deck and prints it
    void Deal(Deck * const dealMe) 
    { 
    	int i = 0;
    	
    	do {
    	printf ("%5s of %-8s\n", getFace (dealMe), getSuit (dealMe));
    	dealMe -> topCard++;
    		i++;
    	} while (i < 6);
    
    	
    	return;
    }
    
    char const * getFace (Deck const * const getMyFace)
    {
    	return getMyFace->myCards[getMyFace->topCard].face;
    }
    
    char const * getSuit (Deck const * const getMySuit)
    {
    	return getMySuit->myCards[getMySuit->topCard].suit;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Just give your cards a value from 1 to 52. Divide that value by 13 to get the suit, and mod that value by 13 to get the face.


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

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    19
    Sure, I understand that. The code will perform that task, it will print out cards. What I want to do is either know a way to access these cards inside the struct, so that I can use them on command, or know how to stick these cards into an array. I know how to use arrays, but unsure how to put something in an array that is in a struct. Bottom line is I will need to use this to compare cards to see what your hand has, and if you win anything.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    struct hand
    {
        char cards[5];
    };
    struct hand mine;
    ...
    if( mine.cards[ 0 ] == something )
    ...
    I'm not really sure what you're asking/having problems with.


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

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    19
    Ok, so for example in my code, I have

    Code:
    typedef struct card { 
    	const char *face; 
    	const char *suit; 
    }Card;
    and then I have

    Code:
    typedef struct {
    	Card myCards[52];
    	unsigned int topCard;
    } Deck;
    In my main then I declare

    Code:
    Deck myDeck;
    Following your explanation, I could do

    Code:
    if (myDeck.myCards[0] == something)
    ...
    Is that correct?
    Last edited by anndruu12; 12-01-2010 at 09:31 PM. Reason: Paren fix.

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    No, because myDeck.myCards[0] is of type struct.
    You can't compare struct with == operator.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing Member from Structure Array
    By rrc55 in forum C Programming
    Replies: 4
    Last Post: 10-18-2009, 06:20 AM
  2. passing structure arrays to functions?
    By bem82 in forum C Programming
    Replies: 3
    Last Post: 10-30-2006, 06:17 AM
  3. Structure - Typedef accessing problem
    By dayknight in forum C Programming
    Replies: 4
    Last Post: 08-11-2006, 11:52 PM
  4. accessing structure pointer problem
    By godhand in forum C Programming
    Replies: 2
    Last Post: 04-09-2004, 10:52 PM
  5. accessing structure issues
    By eth0 in forum C++ Programming
    Replies: 3
    Last Post: 01-12-2004, 01:17 PM