Thread: now i get this error error: ‘(Card *)&(p_player + (sizetype)((long unsigned int)playe

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    662

    now i get this error error: ‘(Card *)&(p_player + (sizetype)((long unsigned int)playe

    what does the above error mean
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    
    enum Suit { SPADES = 0, HEARTS = 1, CLUBS = 2, DIAMONDS = 3};
    enum Card_Value {TWO =2, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE};
    typedef struct
    {
        enum Suit suit;
        enum Card_Value card;
        char card_code[10];
    } Card;
    typedef struct
    {
        char player_name[10];
        Card hand[5];
    }Hand;
    
    void initalize_random_generator(void);
    void shuffle_deck(Card *p_deck);
    char * get_card_code(int suit, int face_value);
    void deal_cards(int player_num, int num_of_cards, int num_of_cards_in_hand, int num_cards_delt, Card *p_deck, Hand *p_player);
    
    int main()
    {
        int num_of_players;
        int i, j;
        unsigned int num_cards_delt = 0;
        Card deck[52];
    
        initalize_random_generator();
        shuffle_deck(deck);
    
        printf("Enter the number of players: ");
        scanf(" %d", &num_of_players);
        // declare variable player array
        Hand player[num_of_players];
    
        for (i = 0; i < num_of_players; i++)
        {
            printf("Please enter player %d's name: ", i + 1);
            scanf(" %s", player[i].player_name);
        }
    
        for (j = 0; j < 5; j++)
        {
            for (i = 0; i < num_of_players; i++)
            {
                deal_cards(num_of_players, 1, j, num_cards_delt, deck, player);
                num_cards_delt++;
            }
        }
    }
    
    void initalize_random_generator(void)
    {
        srand((unsigned) time(NULL));
    }
    
    void shuffle_deck(Card *p_deck)
    {
        int count_cards = 0, card_found = 0;
        int i;
        int suit_value, face_value;
        char *temp_string;
    
        while (count_cards < 52)
        {
            suit_value = rand() % 4;
            face_value = rand() % 13 + 2;
            for (i = 0; i < count_cards + 1; i++)
            {
                if (p_deck[i].suit == suit_value && p_deck[i].card == face_value)
                {
                    card_found = 1;
                    break;
                }
            }
            if (!card_found) //card_found is 0
            {
    /*
                p_deck[count_cards].suit = suit_value;
                p_deck[count_cards].card = face_value;
                temp_string = get_card_code(suit_value, face_value);
                strcpy(p_deck[count_cards].card_code, temp_string));
                count_cards ++;
    //*/
                temp_string = get_card_code(suit_value, face_value);
                p_deck[count_cards] = (Card) {suit_value, face_value};
                strcpy(p_deck[count_cards++].card_code, temp_string);
            }
            card_found = 0;
        }
        for (i = 0; i < 52; i++)
        {
            printf("%s ", p_deck[i].card_code);
        }
    }
    
    char * get_card_code(int suit, int face_value)
    {
    /*******************************************
     * Thanks to flp1969 for this code. Suit:  *
     * goes from  0 spades, 1 hearts ,2 clubs, *
     * and 3 is diamonds. Card: ace = 0, 2 = 1,*
     * 3 = 2 ... 10 = 9, J = 10, Q = 11, K = 12*
     *******************************************/
    
        static char s[5] = { 0 };
    
        int c = 0x1f0a1;
    
        if (face_value == ACE)
        {
            face_value -= 14;
        }
        else
        {
            face_value -= 1;
        }
    
        switch ( suit )
        {   //deliberate fall through
            case 2: // clubs
                c += 0x10;
            case 3: // diamonds
                c += 0x10;
            case 1: // hearts
                c += 0x10;
        }
    
        switch ( face_value )
        {
            case 0 ... 10:
                c += face_value;
                break;
            default:
                c += face_value + 1;
        }
    
        s[0] = 0xf0 | ((c >> 18) & 7);
        s[1] = 0x80 | ((c >> 12) & 0x3f);
        s[2] = 0x80 | ((c >> 6) & 0x3f);
        s[3] = 0x80 | (c & 0x3f);
    
        return s;
    }
    
    void deal_cards(int player_num, int num_of_cards, int num_of_cards_in_hand, int num_cards_delt, Card *p_deck, Hand *p_player)
    {
        int i;
    
        for (i = 0; i < num_of_cards; i++)
        {
            strcpy(p_player[player_num].hand.card_code, p_deck[num_cards_delt].card_code);
        }
    }
    coop

  2. #2
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    please someone tell me what the difference is between
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct
    {
        int age;
        char location[20];
    } Details;
    
    typedef struct
    {
        int id_number;
        Details information;
    } Record;
    
    void copy_info(int num_record, Record *p_data, Details *p_person)
    {
        int i;
    
        for (i = 0; i < num_record; i++)
        {
            p_data[i].information.age = p_person[i].age;
            strcpy(p_data[i].information.location, p_person[i].location);
        }
    }
    }
    which works wonderfully and this function

    Code:
    void deal_cards(int player_num, int num_of_cards, int num_of_cards_in_hand, int num_cards_delt, Card *p_deck, Hand *p_player)
    {
        int i;
    
        for (i = 0; i < num_of_cards; i++)
        {
            strcpy(p_player[player_num].hand.card_code, p_deck[num_cards_delt].card_code)
        }
    }
    which throws the above error
    coop

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,083
    Maybe the problem actually lies here:
    Code:
    typedef struct
    {
        char player_name[10];
        Card hand[5];
    }Hand;
    A hand has 5 Card objects, but for some reason you decided to call the array of Card objects hand instead of cards. Suppose we fixed that:
    Code:
    typedef struct
    {
        char player_name[10];
        Card cards[5];
    }Hand;
    Now do you see the problem with:
    Code:
    strcpy(p_player[player_num].cards.card_code, p_deck[num_cards_delt].card_code);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    nope only that i now have two variables called card ie Card.card and Hand.card

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,083
    No, cards is plural. The idea is that you have a Hand object that has cards. You don't have a Hand object that has a hand; the Hand object is a hand (or we say that it models a hand of cards). Likewise, I would change Card to:
    Code:
    typedef struct
    {
        enum Suit suit;
        enum Card_Value value;
        char code[10];
    } Card;
    Because you have a Card object that has a suit, value, and a code. The Card object doesn't have a card; the Card object is a card (or we say that it models a card).

    Okay, here's another one:
    Code:
    int numbers[5] = {0};
    printf("%u\n", numbers.count);
    What's wrong with the above code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    count hasnt been initiated or declared and numbers is an int array not a member array in a struct

  7. #7
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    not sure if this is an error but apart from the above issues with numbers.count you are passing it into a unsigned int (%u)

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,083
    Ok, you're just being dense. Arrays do not have members. The problem with your code is that this is an array: p_player[player_num].hand

    It doesn't have a card_code member because it is an array. You have to write something like: strcpy(p_player[player_num].hand[0].card_code
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    like this???
    strcpy(p_player[player_num].cards[num_of_cards_in_hand].code, p_deck[num_cards_delt].code);

  10. #10
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    that one leads us back to a previously posted topic about how that changes the value of num_cards_delt to a massive negative number or sometimes massive big number or sometimes changes the value of i in the for loop calling the function

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,083
    Yes.

    I think your deal_cards function doesn't need so many parameters. You need a Hand* parameter, but it should point to the hand of the player that you want to deal the cards to, so you don't
    need player_num. num_of_cards_in_hand shouldn't be a parameter: it should be a member of Hand:
    Code:
    typedef struct
    {
        char player_name[10];
        Card cards[5];
        size_t num_of_cards;
    }Hand;
    Furthermore, the loop should have an additional check to ensure that the number of cards in the hand doesn't exceed 5 (which shouldn't be a magic number).

    As for your massive negative number problem: you're probably doing something like writing to an array out of bounds. As flp1969 suggested, you should be using a debugger.

    By the way, the word is "dealt", not "delt".
    Last edited by laserlight; 05-11-2019 at 04:55 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    number_in_hand is there so after the first round of betting and the players want to change cards and i deal them new cards it doesn't overwrite the cards the player wanted to keep. player num is the number of players that the user enters so i need to know how many players there are in order to know how many hands to deal.

    the debugger doesn't explain why on leaving the function num_cards_dealt is still zero but the second it goes to the next line in main it changes to a negative number. nor does it explain why when one of my goes at correcting the issue i did away with num_cards_dealt, changed p_deck[num_cards_dealt] to p_deck[0] and removed the variable from the function deceleration so the function knew nothing about it and incremented num_cards_dealt separately in a completely different for loop it was still changed to a negative number.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,083
    Quote Originally Posted by cooper1200
    number_in_hand is there so after the first round of betting and the players want to change cards and i deal them new cards it doesn't overwrite the cards the player wanted to keep.
    I'm not saying to remove it, I'm saying it should be a member of the Hand struct. In fact, if it isn't a member of Hand, you're going to find it more difficult to do what you just described.

    Quote Originally Posted by cooper1200
    player num is the number of players that the user enters so i need to know how many players there are in order to know how many hands to deal.
    I'm not saying get rid of player num; I'm saying get rid of the player_num parameter by making better use of the Hand* parameter. It should point to the current player's hand, not just be a pointer to the first hand in your array of Hand objects. This is separation of concerns: the function is dealing cards to a hand. It doesn't care how many hands there are, as long as it is given that one hand to work with.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    if i replace strcpy(p_player[player_num].cards[num_of_cards_in_hand].code, p_deck[num_cards_delt].code) with printf("p_player[player_num].hand[num_of_cards_in_hand].card_code is %s to here\n", p_player[player_num].hand[num_of_cards_in_hand].card_code) or with printf("p_deck[num_cards_dealt].code %s \n", p_deck[num_cards_dealt].code) it works no issues it is being passed the same parameters as the strcpy version.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 03-05-2018, 09:59 AM
  2. Generate unsigned long long type random numbers
    By patishi in forum C Programming
    Replies: 27
    Last Post: 09-11-2013, 09:03 PM
  3. Replies: 1
    Last Post: 10-11-2010, 01:53 AM
  4. STLport with MingW - Long Long error on project build
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 08-21-2006, 08:55 AM
  5. Converting an unsigned int to to unsigned long long (64 bit)
    By ninjacookies in forum C Programming
    Replies: 18
    Last Post: 02-11-2005, 12:09 PM

Tags for this Thread