Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6906277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:17:21+00:00 2026-05-27T08:17:21+00:00

Please feel free to copy paste and try it out, it stops working halfway

  • 0

Please feel free to copy paste and try it out, it stops working halfway through function handget when individual hand is assigned a space in memory

/* 
 * A program that is meant to classify five-card poker hands. 
 *Is still in progress
 */

#include<stdio.h>
#include<math.h>
#include<ctype.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>

#define SEED 7
#define SIZE 5

/*
 *
 */

typedef enum suits {
   clubs,
   diamonds,
   hearts,
   spades
} suits;

typedef enum values {
    zero,
    one, // not used, but ensures numerical values correspond
    two,
    three,
    four,
    five,
    six,
    seven,
    eight,
    nine,
    ten,
    jack,
    queen,
    king,
    ace
} values; 

typedef struct cards {
    suits suit;
    values value;
} cards;

int islegal(cards *hand, int nr_of_cards);
/* Given any number of cards, determines whether any duplicates
 * or false cards are present or not. If so, returns 0, otherwise 1. 
 */
int flop(cards **handPtr); 
/* Asks the user to input a hand of cards; 
 * returns the number of cards being input
 */
int *rawtoflop (cards *handPtr, int nr_of_cards);

int hander(cards **handPtr,int counter); 

int handget(cards **playerhands,cards *thishands, int handsize);

void printsuit(suits thissuit);
/* Prints the enum-type suits
 */

void printvalue(values thisvalue);
/* Prints the enum-type values
 */

void printhand(cards *hand, int nr_of_cards); 
/* Prints a hand of cards without further processing 
*/
int main(void) {
    cards *thishand, *playerhands;

    flop(&thishand);
    printhand(thishand,6);
    handget(&playerhands,thishand,6);

    return 0;
}

int islegal(cards *hand, int nr_of_cards) {
    int i, fulldeck[4][13]={0};
    int current_value, current_suit;
    cards *current_card = hand;
    int legal = 1; 

    for (i=0; i<nr_of_cards; i++) {
        current_value = (int) (*current_card).value;
        current_suit = (*current_card).suit;

        // if the current card has value zero, it is not a valid hand
        if (current_value==0) {
            legal = 0;
            break;
        }

        //check if the current card already appears, if yes invalid hand, if no, 
        //change the flag for that card in the full deck. 
        //Since (two) will correspond to fulldeck[.][0] there is a -2 offset.
        if ( (fulldeck[current_suit][current_value - 2]) > 0 ) {
            legal = 0;
            break;
        } else {
            fulldeck[current_suit][current_value - 2]++;
        }
        current_card++;
    }

    return legal;
}

int flop(cards **handPtr) {
    int i,*q=NULL,n=NULL;
    int j[5]={0,0,0,0,0};
    int k[5]={1,1,1,1,1},nr_of_cards = 5;//for more/less cards CHANGE HERE!
    char rawsuit, rawcard[4];

    // allocate the required amount of memory for your cards
    (*handPtr) = (cards *) malloc(nr_of_cards * sizeof(cards));

    n=memcmp(j,k,sizeof(j));
    while(n!=0) {
        printf("Please input the five cards on the table: ");
        q=rawtoflop(*handPtr,nr_of_cards);
        for (i=0;i<nr_of_cards;i++) {
            j[i]=*(q+i);
        }
        n=memcmp(j,k,sizeof(j));
    }

    free(&handPtr);
    return nr_of_cards;
}

int *rawtoflop (cards *handPtr, int nr_of_cards){
    int i,m[5],*mPtr;
    char rawsuit, rawcard[4];

    mPtr=m;
        // ask for the cards
    for (i=0; i<nr_of_cards; i++)/* do */{

        scanf("%3s", &rawcard);

        rawsuit = rawcard[0]; 

        if (rawcard[1]=='1') {
            if (rawcard[2]=='0') {
                (handPtr)[i].value = ten;
            } else {
                (handPtr)[i].value = zero;
            }
        } else if (rawcard[1]=='2') {
            (handPtr)[i].value = two;
        } else if (rawcard[1]=='3') {
            (handPtr)[i].value = three;
        } else if (rawcard[1]=='4') {
            (handPtr)[i].value = four;
        } else if (rawcard[1]=='5') {
            (handPtr)[i].value = five;
        } else if (rawcard[1]=='6') {
            (handPtr)[i].value = six;
        } else if (rawcard[1]=='7') {
            (handPtr)[i].value = seven;
        } else if (rawcard[1]=='8') {
            (handPtr)[i].value = eight;
        } else if (rawcard[1]=='9') {
            (handPtr)[i].value = nine;
        } else if (rawcard[1]=='J') {
            (handPtr)[i].value = jack;
        } else if (rawcard[1]=='Q') {
            (handPtr)[i].value = queen;
        } else if (rawcard[1]=='K') {
            (handPtr)[i].value = king;
        } else if (rawcard[1]=='A') {
            (handPtr)[i].value = ace;
        } else {
            (handPtr)[i].value = zero;
        }

        switch (rawsuit) {
            case 'h':
                (handPtr)[i].suit = hearts; 
                break;
            case 'd':
                (handPtr)[i].suit = diamonds;
                break;
            case 'c':
                (handPtr)[i].suit = clubs;
                break;
            case 's':
                (handPtr)[i].suit = spades;
                break;
            default:
                (handPtr)[i].value = zero;
        }

        m[i]=(islegal(handPtr,i+1));
    }
    return mPtr;
}


int hander(cards **handPtr,int counter) {
    int i, nr_of_cards = 2;
    char rawsuit, rawcard[4];

    if(counter==0){
        // allocate the required amount of memory for your cards
        (*handPtr) = (cards *) malloc(nr_of_cards * sizeof(cards));
    }
    // ask for the cards
    for (i=0; i<nr_of_cards; i++) do {

        scanf("%3s", &rawcard);

        rawsuit = rawcard[0]; 

        if (rawcard[1]=='1') {
            if (rawcard[2]=='0') {
                (*handPtr)[i].value = ten;
            } else {
                (*handPtr)[i].value = zero;
            }
        } else if (rawcard[1]=='2') {
            (*handPtr)[i].value = two;
        } else if (rawcard[1]=='3') {
            (*handPtr)[i].value = three;
        } else if (rawcard[1]=='4') {
            (*handPtr)[i].value = four;
        } else if (rawcard[1]=='5') {
            (*handPtr)[i].value = five;
        } else if (rawcard[1]=='6') {
            (*handPtr)[i].value = six;
        } else if (rawcard[1]=='7') {
            (*handPtr)[i].value = seven;
        } else if (rawcard[1]=='8') {
            (*handPtr)[i].value = eight;
        } else if (rawcard[1]=='9') {
            (*handPtr)[i].value = nine;
        } else if (rawcard[1]=='J') {
            (*handPtr)[i].value = jack;
        } else if (rawcard[1]=='Q') {
            (*handPtr)[i].value = queen;
        } else if (rawcard[1]=='K') {
            (*handPtr)[i].value = king;
        } else if (rawcard[1]=='A') {
            (*handPtr)[i].value = ace;
        } else {
            (*handPtr)[i].value = zero;
        }

        switch (rawsuit) {
            case 'h':
                (*handPtr)[i].suit = hearts; 
                break;
            case 'd':
                (*handPtr)[i].suit = diamonds;
                break;
            case 'c':
                (*handPtr)[i].suit = clubs;
                break;
            case 's':
                (*handPtr)[i].suit = spades;
                break;
            default:
                (*handPtr)[i].value = zero;
        }
    } while (!islegal(*handPtr, i+1));

    return nr_of_cards;
}

int handget(cards **playerhand,cards *thishands, int handsize) {

    cards *player=NULL,*individualhand=NULL;
    int nr_players=1;

    (*playerhand) = (cards *) malloc(7*sizeof(cards));

    memcpy(*playerhand,thishands,handsize*sizeof(cards));

    printf("Please enter the cards for player 1: ");

    hander(&player,0);
    memcpy(*playerhand+5,player,7*sizeof(cards));

        printf("1 we made it this far chaps!!!\n");
        individualhand =(cards *) malloc(7*sizeof(cards));//THIS IS WHERE IT ALL GOES WRONG!!
        printf("2 we made it this far chaps!!\n");

    return nr_players;
}

void printsuit(suits thissuit) {
    switch (thissuit) {
        case diamonds:
            printf("d");
            break;
        case clubs:
            printf("c");
            break;
        case hearts:
            printf("h");
            break;
        case spades:
            printf("s");
            break;
        }
    return;
}

void printvalue(values thisvalue) {
    switch (thisvalue) {
        case two:
            printf("2");    
            break;
        case three:
            printf("3");
            break;
        case four:
            printf("4");
            break;
        case five:
            printf("5");
            break;
        case six:
            printf("6");
            break;
        case seven:
            printf("7");
            break;
        case eight:
            printf("8");
            break;
        case nine:
            printf("9");
            break;
        case ten:
            printf("10");
            break;
        case jack:
            printf("J");
            break;
        case queen:
            printf("Q");
            break;
        case king:
            printf("K");
            break;
        case ace:
            printf("A");
            break;
    }

    return;
}


void printhand(cards *hand, int nr_of_cards) {
    int i;

    for (i=0; i<nr_of_cards; i++) {
        printsuit((hand[i]).suit);
        printvalue((hand[i]).value);
        printf(" ");
    }
    printf("\n");

    return;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:17:22+00:00Added an answer on May 27, 2026 at 8:17 am

    You allocate enough space for 7 cards:

    (*playerhand) = (cards *) malloc(7*sizeof(cards));
    

    Then you copy seven cards into *playerhand+5:

    memcpy(*playerhand+5,player,7*sizeof(cards));
    

    So you get a buffer overflow. *playerhand+5 is the same as &(*playerhand)[5]. So you’re copying 7 cards into the fifth place in an array with enough capacity for 7 cards.

    That’s one problem of using magic number. I can’t guess what this “5” means if you don’t give it a name (nor why 7 cards).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please feel free to correct me if I am wrong at any point... I
Please feel free to move this to meta/superuser if this is the wrong place.
This is a question I intend to answer myself, but please feel free to
I'm new to git so please feel free to RTFM me... I have multiple
Please feel free to rephrase the title if it is not appropriate. I am
Please feel free to add multiple answers, each with a single point, to make
(This question is not really restricted to the language so please feel free to
I really have no idea what to title this so someone PLEASE feel free
Background / Disclaimer First of all, please feel free to skip this entirely if
If the title isn't accurate enough, please feel free to change it to something

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.