Thread: Array of structs, array of pointers that point to structs and more...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    3

    Array of structs, array of pointers that point to structs and more...

    Hello!

    I am trying to fill a deck of cards and I encounter a problem that I think has to do with the printf() function printing cardPtr[k].face.

    I get no compiling errors, but my console crashes.

    I am trying (I think) to make an array of 52 structs and an array of pointers to these structs. Then printing out the values of the members of all the 52 structs I am pointing to.

    Need some help! =/


    Code:
    #include <stdio.h>
    
    struct card {
        const char *face;
        const char *suit;
    };
    
    typedef struct card Card;
    
    
    int main(void)
    {
        //Initializing array of 52 structers of type Card and pointers to these
        Card deck[52];
        Card *cardPtr[52];
    
        int j=0;
        for(j;j<52;j++) cardPtr[j] = &deck[j];
    
        //Initializing array of suit and face
        const char *suit[] = { "Hearts", "Spades", "Diamonds", "Clubs" };
        const char *face[] = { "Ace", "Duce", "Three", "Four", "Five", "Six", "Seven", "Eigth", "Nine", "Ten", "Jack", "Queen", "King" };
        
        int i=0;
        for(i;i<52;i++)
        {
            deck[i].suit = suit[ i % 13 ];
            deck[i].face = face[ i / 13 ];
        }
    
        int k=1;
        for(k;k<52;k++) printf("%s of %s\n", cardPtr[k].face, cardPtr[k].suit);
        getchar();
        return 0;
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Study this... It may help....
    Code:
    // deck of cards demonstration
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    #define CARDS 52
    
    const char *face[] = {"Ace","Two","Three","Four","Five","Six","Seven",
                          "Eight","Nine","Ten","Jack","Queen","King" };
    
    const char *suit[] = {"Spades","Hearts","Clubs","Diamonds"};
    
    
    // function to shuffle the deck
    int Shuffle(int *deck)
      { 
        int i, temp, card; 
    
        printf("\nShuffling...\n\n");
        for (i = CARDS - 1; i > 0; i--)
          { 
            card = rand() % i;
            temp = deck[i];
            deck[i] = deck[card];
            deck[card] = temp;
    
          }
        return 0;
      }
          
    
    // program entry point
    int main (void)
      { 
        int deck[CARDS];        // deck
        int card = CARDS + 1;   // card
        int fac, sut;           // face and suit
        int i;                  // counter
        char ch = 0;            // keyboard
        
        srand(time(NULL));
    
        printf("Card dealer simulation... \n\n");
    
        //initialize the deck
        for (i = 0; i < CARDS; i++)
          deck[i] = i;
    
        // deal cards  
        do
          {
            if ( card >= CARDS )  
              card = Shuffle(deck);
    
            // face and suit
            fac = deck[card] % 13;
            sut = deck[card] / 13;
    
            // display the card
            printf("%2d > %s of %s\t\t",deck[card],face[fac],suit[sut]);
            
            // set up for next card
            card++;
    
            // wait for user input
            printf("(Enter to deal, Q to quit)\n");
            ch = getchar();
          }
        while( toupper(ch) != 'Q');
        return 0;
      }

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    3
    I see how your code will have the same function as what I am trying to achieve, but I am just doing this to learn C so I want to drill myself on structs and pointers. What I really want to know is if

    for(k;k<52;k++) printf("%s of %s\n", cardPtr[k].face, cardPtr[k].suit);

    will work when cardPtr[] is an array of pointers that point to a struct.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Quote Originally Posted by Phataas View Post
    I see how your code will have the same function as what I am trying to achieve, but I am just doing this to learn C so I want to drill myself on structs and pointers. What I really want to know is if

    for(k;k<52;k++) printf("%s of %s\n", cardPtr[k].face, cardPtr[k].suit);

    will work when cardPtr[] is an array of pointers that point to a struct.
    The best way to find if something will work is by trying it...

    But the way you have it set up, not necessarily. Try:

    Code:
    Card cardPtr[52];
    
    /* or */
    
    Card *cardPtr = malloc(52);

  5. #5
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    The two biggest problems with your code are,

    With pointer to struct, you need to use the member indirection operator ->
    and not the member operator .

    And, your division and modulo operations are backwards.

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    3
    Goddamit! I hate and love C at the same time I just had to change the division and modulo with eachother and change to arrow instead of dot operator. Thank you all very much for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of pointers to structs
    By curiosita in forum C Programming
    Replies: 0
    Last Post: 04-30-2011, 04:08 PM
  2. Replies: 3
    Last Post: 03-31-2009, 12:34 PM
  3. Array of Pointers to Structs
    By I BLcK I in forum C++ Programming
    Replies: 3
    Last Post: 03-15-2008, 11:03 PM
  4. Array of pointers to structs
    By lpmrhahn in forum C Programming
    Replies: 1
    Last Post: 04-17-2004, 09:46 AM
  5. array of pointers to structs
    By stumon in forum C Programming
    Replies: 7
    Last Post: 03-24-2003, 07:13 AM