Hello,
Im writing some code for a very simple card game and need to set up a deck.
To do this I have created a structure to hold the card information (shown below)
To create the deck i call the following function from main.Code:typedef struct
{
int suit;
int value;
}card;
I also need to shuffle this deck, and just to do this i am gng to switch two elements of deck using a random number generator and call the function a few times, however i havent got to that stage yet, so just to test if i am doing it correctly i have set up the following shuffle functionCode:card create_deck (void){
int x = 0,i,j;
card *deck;
deck = (card*) malloc (DECKSIZE * sizeof(card));
if ( deck == NULL) {
fprintf( stderr, "Memory allocation failed\n"); }
else {
for (i = 1; i <= 4; i++) {
for (j = 2; j < 15; j++) {
deck[x].suit = i;
deck[x].value = j;
x++;
}
}
}
return *deck;
}
}
Now the problem is i dont know how, from main, im meant to pass the deck or a pointer to that deck i have created to the shuffle function, below is what my main looks like.Code:card shuffle_deck (card deck)
{
int x = 6;
int y = 9;
card temp;
//store values in deck[x] in temp
temp.suit = deck[x].suit;
temp.value = deck[x].value;
//copy values from deck[y] into deck[x]
deck[x].suit = deck[y].suit;
deck[x].value = deck[y].value;
//copy values from temp to deck[y]
deck[y].suit = temp.suit;
deck[y].value = temp.value;
}
int main (void)
Not sure if im going about this the right way or not,Code:{
card p;
p = create_deck();
shuffle_deck(p);
return 0;
}
Please help.

