daily pastebin goal
84%
SHARE
TWEET

Untitled

a guest Aug 11th, 2018 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Assigning address to NULL pointer using malloc is making program freeze? This is probably quite basic?
  2. int hander(cards **handPtr,int counter) {
  3.     int i, nr_of_cards = 2;
  4.     char rawsuit, rawcard[4];
  5.  
  6.     if(counter==0){
  7.         // allocate the required amount of memory for your cards
  8.         (*handPtr) = (cards *) malloc(nr_of_cards * sizeof(cards));
  9.     }
  10.     // ask for the cards
  11.     for (i=0; i<nr_of_cards; i++) do {
  12.  
  13.         scanf("%3s", &rawcard);
  14.  
  15.         rawsuit = rawcard[0];
  16.  
  17.         if (rawcard[1]=='1') {
  18.             if (rawcard[2]=='0') {
  19.                 (*handPtr)[i].value = ten;
  20.             } else {
  21.                 (*handPtr)[i].value = zero;
  22.             }
  23.         } else if (rawcard[1]=='2') {
  24.             (*handPtr)[i].value = two;
  25.         } else if (rawcard[1]=='3') {
  26.             (*handPtr)[i].value = three;
  27.         } else if (rawcard[1]=='4') {
  28.             (*handPtr)[i].value = four;
  29.         } else if (rawcard[1]=='5') {
  30.             (*handPtr)[i].value = five;
  31.         } else if (rawcard[1]=='6') {
  32.             (*handPtr)[i].value = six;
  33.         } else if (rawcard[1]=='7') {
  34.             (*handPtr)[i].value = seven;
  35.         } else if (rawcard[1]=='8') {
  36.             (*handPtr)[i].value = eight;
  37.         } else if (rawcard[1]=='9') {
  38.             (*handPtr)[i].value = nine;
  39.         } else if (rawcard[1]=='J') {
  40.             (*handPtr)[i].value = jack;
  41.         } else if (rawcard[1]=='Q') {
  42.             (*handPtr)[i].value = queen;
  43.         } else if (rawcard[1]=='K') {
  44.             (*handPtr)[i].value = king;
  45.         } else if (rawcard[1]=='A') {
  46.             (*handPtr)[i].value = ace;
  47.         } else {
  48.             (*handPtr)[i].value = zero;
  49.         }
  50.  
  51.         switch (rawsuit) {
  52.             case 'h':
  53.                 (*handPtr)[i].suit = hearts;
  54.                 break;
  55.             case 'd':
  56.                 (*handPtr)[i].suit = diamonds;
  57.                 break;
  58.             case 'c':
  59.                 (*handPtr)[i].suit = clubs;
  60.                 break;
  61.             case 's':
  62.                 (*handPtr)[i].suit = spades;
  63.                 break;
  64.             default:
  65.                 (*handPtr)[i].value = zero;
  66.         }
  67.     } while (!islegal(*handPtr, i+1));
  68.  
  69.     return nr_of_cards;
  70. }
  71.  
  72. int handget(cards **playerhand,cards *thishands, int handsize) {
  73.  
  74.     cards *player,*individualhand=NULL;
  75.     int i,playerhandsize=2,j=0,nr_players,phandsize,counter=0, individualhandsize;
  76.  
  77.     printf("Please enter the number of players: ");
  78.     scanf("%d",&nr_players);
  79.  
  80.     (*playerhand) = (cards *) malloc(((playerhandsize*nr_players)*sizeof(cards))+(handsize*sizeof(cards)));
  81.  
  82.     handcat(playerhand,thishands,handsize);
  83.  
  84.     for (i=1;i<=nr_players;i++){
  85.  
  86.         do {
  87.             printf("Please enter the cards for player %d: ",i);
  88.             phandsize = (i*hander(&player,counter))+handsize;
  89.             playerflopcat(playerhand,player,j,phandsize,handsize);
  90.             counter++;
  91.         } while (!islegal(*playerhand, phandsize));
  92.         j++;
  93.  
  94.     }
  95.     printf("The cards on the table are:n");
  96.     printhand(thishands,handsize);
  97.  
  98.     for (i=1;i<=nr_players;i++){    
  99.         printf("The private cards of player %d are ",i);
  100.         printhand(*playerhand+(handsize+(2*(i-1))),2);
  101.     }
  102.  
  103.     for (i=1;i<=nr_players;i++){    
  104.  
  105.         individualhandsize = handsize + playerhandsize;
  106.         printf("individualhandsize=%in",individualhandsize);
  107.  
  108.         if(individualhand==NULL){
  109.         printf("individualhand EQUALS NULL!");
  110.         }
  111.  
  112.         individualhand=((cards *) malloc(individualhandsize*sizeof(cards)));//THIS IS WHERE IT ALL GOES WRONG!!
  113.         printf("made it this far chaps!!n");
  114.  
  115.         memcpy(individualhand,playerhand,individualhandsize*sizeof(cards));
  116.         printf("made it this far chaps!!n");
  117.  
  118.         printf("The cards available to player %d are ",i);
  119.         /*i have yet to add code here*/
  120.  
  121.     }
  122.  
  123.     return nr_players;
  124. }
  125.  
  126. void handcat(cards **playerhand,cards *playerx, int nr_cards){
  127.  
  128.     memcpy(*playerhand,playerx,nr_cards*sizeof(cards));
  129.  
  130.     return;
  131. }
  132.  
  133.  
  134. void playerflopcat(cards **playerhand,cards *playerx,int j, int nr_cards,int handsize){
  135.  
  136.     memcpy((*playerhand+(handsize+(2*j))),playerx,nr_cards*sizeof(cards));
  137.  
  138.     return;
  139. }
  140.    
  141. int main(void) {
  142.     int handsize=0, playerhandsize=0, nr_of_players=0;
  143.     cards *thishand, *playerhands;
  144.  
  145.     handsize = flop(&thishand);
  146.     //flop asks user for list of cards
  147.     //user inputs(e.g. "d5 d6 d7 d8 d9")
  148.     //flop returns int handsize=5
  149.  
  150.     printhand(thishand,handsize);
  151.     //printhand returns a list (of handsize length) of the cards making up thishand
  152.     //e.g. "d5 d6 d7 d8 d9"
  153.  
  154.     nr_of_players = handget(&playerhands,thishand,handsize);
  155.     //handget is the function we're looking at
  156.     //takes thishand which was assigned (using function flop) to a space in memory containing list of cards ("d5 d6 d7 d8 d9")
  157.     //also uses int handsize = 5
  158.     //playerhands is assigned in function handget
  159.  
  160.     //  printhand(playerhands,(handsize+(nr_of_players*2)));
  161.  
  162.     return 0;
  163. }
  164.    
  165. int flop(cards **handPtr) {
  166.     int i,*q=NULL,n=NULL;
  167.     int j[5]={0,0,0,0,0};
  168.     int k[5]={1,1,1,1,1},nr_of_cards = 5;//for more/less cards CHANGE HERE!
  169.     char rawsuit, rawcard[4];
  170.  
  171.     // allocate the required amount of memory for your cards
  172.     (*handPtr) = (cards *) malloc(nr_of_cards * sizeof(cards));
  173.  
  174.     n=memcmp(j,k,sizeof(j));
  175.     while(n!=0) {
  176.         printf("Please input the five cards on the table: ");
  177.         q=rawtoflop(*handPtr,nr_of_cards);
  178.         for (i=0;i<nr_of_cards;i++) {
  179.             j[i]=*(q+i);
  180.         }
  181.         n=memcmp(j,k,sizeof(j));
  182.     }
  183.     return nr_of_cards;
  184. }
  185.    
  186. int islegal(cards *hand, int nr_of_cards) {
  187.     int i, fulldeck[4][13]={0};
  188.     int current_value, current_suit;
  189.     cards *current_card = hand;
  190.     int legal = 1;
  191.  
  192.     for (i=0; i<nr_of_cards; i++) {
  193.         current_value = (int) (*current_card).value;
  194.         current_suit = (*current_card).suit;
  195.  
  196.         // if the current card has value zero, it is not a valid hand
  197.         if (current_value==0) {
  198.             legal = 0;
  199.             break;
  200.         }
  201.  
  202.         //check if the current card already appears, if yes invalid hand, if no,
  203.         //change the flag for that card in the full deck.
  204.         //Since (two) will correspond to fulldeck[.][0] there is a -2 offset.
  205.         if ( (fulldeck[current_suit][current_value - 2]) > 0 ) {
  206.             legal = 0;
  207.             break;
  208.         } else {
  209.             fulldeck[current_suit][current_value - 2]++;
  210.         }
  211.         current_card++;
  212.     }
  213.  
  214.     return legal;
  215. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top