Note that there are some explanatory texts on larger screens.

plurals
  1. POPointer assignment using malloc going wrong
    primarykey
    data
    text
    <p>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 </p> <pre><code>/* * A program that is meant to classify five-card poker hands. *Is still in progress */ #include&lt;stdio.h&gt; #include&lt;math.h&gt; #include&lt;ctype.h&gt; #include&lt;stdlib.h&gt; #include&lt;assert.h&gt; #include&lt;string.h&gt; #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(&amp;thishand); printhand(thishand,6); handget(&amp;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&lt;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]) &gt; 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&lt;nr_of_cards;i++) { j[i]=*(q+i); } n=memcmp(j,k,sizeof(j)); } free(&amp;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&lt;nr_of_cards; i++)/* do */{ scanf("%3s", &amp;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&lt;nr_of_cards; i++) do { scanf("%3s", &amp;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(&amp;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&lt;nr_of_cards; i++) { printsuit((hand[i]).suit); printvalue((hand[i]).value); printf(" "); } printf("\n"); return; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload