5 Replies - 249 Views - Last Post: 12 May 2019 - 02:34 AM Rate Topic: -----

#1 cooper1200   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-May 19

value of variable changes to a huge number after function called

Posted 11 May 2019 - 10:25 AM

hi all i am trying to write a poker game i have got to the stage of begining to deal the cards to the players but i get a segmentation fault on the printf statement. when debugging and going through line by line "num_cards_dealt" changed from 0 to a number in the billions after returning from the function to get the card.
here is the 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_index);
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, unsigned int num_cards_delt, Card *p_deck_index, Hand 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);
            printf("%s ", player[i].hand[j].card_code);
            num_cards_delt++;
        }
    }
}

void initalize_random_generator(void)
{
    srand((unsigned) time(NULL));
}

void shuffle_deck(Card *p_deck_index)
{
    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_index[i].suit == suit_value && p_deck_index[i].card == face_value)
            {
                card_found = 1;
                break;
            }
        }
        if (!card_found) //card_found is 0
        {
/*
            p_deck_index[count_cards].suit = suit_value;
            p_deck_index[count_cards].card = face_value;
            temp_string = get_card_code(suit_value, face_value);
            strcpy(p_deck_index[count_cards].card_code, temp_string));
            count_cards ++;
//*/
            temp_string = get_card_code(suit_value, face_value);
            p_deck_index[count_cards] = (Card) {suit_value, face_value};
            strcpy(p_deck_index[count_cards++].card_code, temp_string);
        }
        card_found = 0;
    }
    for (i = 0; i < 52; i++)
    {
        printf("%s ", p_deck_index[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, unsigned int num_cards_delt, Card *p_deck_index, Hand player[])
{
    int i;

    for (i = 0 + num_of_cards_in_hand; i < num_of_cards + num_of_cards_in_hand; i++)
    {
        strcpy(player[player_num].hand[i].card_code, p_deck_index[num_cards_delt].card_code);
    }
}


many thanks
coop

Is This A Good Question/Topic? 0
  • +

Replies To: value of variable changes to a huge number after function called

#2 jimblumberg   User is offline

  • member icon

Reputation: 5731
  • View blog
  • Posts: 17,558
  • Joined: 25-December 09

Re: value of variable changes to a huge number after function called

Posted 11 May 2019 - 10:32 AM

Also posted here.
Was This Post Helpful? 2
  • +
  • -

#3 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6913
  • View blog
  • Posts: 23,499
  • Joined: 05-May 12

Re: value of variable changes to a huge number after function called

Posted 11 May 2019 - 01:22 PM

Looks like a incorrect parameters being passed on line number 49 is causing out of bounds writes in line 156.
Was This Post Helpful? 0
  • +
  • -

#4 cooper1200   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-May 19

Re: value of variable changes to a huge number after function called

Posted 11 May 2019 - 02:04 PM

i changed the decleratopn to void deal_cards(int player_num, int num_of_cards, int num_of_cards_in_hand, unsigned int num_cards_delt, Card *p_deck_index, Hand *p_player) but now i get an error saying ||=== Build: |error: ‘(Card *)&(whogivesashit + (sizetype)((long unsigned int)player_num * 112))->hand’ is a pointer;
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6913
  • View blog
  • Posts: 23,499
  • Joined: 05-May 12

Re: value of variable changes to a huge number after function called

Posted 11 May 2019 - 06:12 PM

Please post your updated code.

Anyway, the problem that I was alluding to in your previous code was not that the declaration was wrong, but rather you were passing in the wrong values as parameters. For example, the first parameter you were passing in num_of_players. Since arrays are zero based in C, your player array is accessed out of range on line 156 with this expression: player[player_num]. There are other similar potential out of range accesses with regards to the other parameters and how they are used.
Was This Post Helpful? 0
  • +
  • -

#6 cooper1200   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-May 19

Re: value of variable changes to a huge number after function called

Posted 12 May 2019 - 02:34 AM

dang i was just about to say that num_of_players is limited by the for loop on line 47 but i passed in num_of_players not i (the for loop variable) i can be thick at times sorry thank your for your help
here is the updated 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 value;
    char code[10];
} Card;
typedef struct
{
    char player_name[10];
    Card cards[5];
}Hand;

void initalize_random_generator(void);
void shuffle_deck(Card *p_deck);
void get_card_code(int suit, int face_value, char *p_temp_string);
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;
    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(i, 1, j, num_cards_delt, deck, player);
            num_cards_delt++;
        }
    }

    for (j = 0; j < num_of_players; j++)
    {
         printf("player %d has ", j);
        for (i = 0; i < 5; i++)
        {
            printf("%s ", player[j].cards[i].code); // prints out each players cards
        }
        printf("\n");
    }
}

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[5];

    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].value == 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 ++;
//*/
            get_card_code(suit_value, face_value, temp_string);
            p_deck[count_cards] = (Card) {suit_value, face_value};
            strcpy(p_deck[count_cards++].code, temp_string);
        }
        card_found = 0;
    }
    for (i = 0; i < 52; i++)
    {
        printf("%s ", p_deck[i].code);
    }
}

void get_card_code(int suit, int face_value, char *p_temp_string)
{
/*******************************************
 * 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*
 *******************************************/

    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;
    }

    p_temp_string[0] = 0xf0 | ((c >> 18) & 7);
    p_temp_string[1] = 0x80 | ((c >> 12) & 0x3f);
    p_temp_string[2] = 0x80 | ((c >> 6) & 0x3f);
    p_temp_string[3] = 0x80 | (c & 0x3f);
}

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].cards[num_of_cards_in_hand].code, p_deck[num_cards_delt].code);
        //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);
    }
}


thanks again
coop
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1