I am having some difficulties writing a simple function for a program I am writing. The program is a card game, and I have a struct defined as follows:
typedef struct {int rank; char suit;} card;
I am trying to write a function that will swap the values of 2 variables of type card. My efforts so far look like this:
void SwapCards (card *card1, card *card2)
{
card temp_card;
temp_card = *card1;
*card1 = *card2;
*card2 = temp_card;
}
the problem is that this does not work.
I have tried using it as follows:
card card10, card20;
card10.suit='c';
card10.rank=12;
card20.suit='d';
card20.rank=10;
SwapCards(*card10, *card20);
Please tell me I don't need to declare pointer variables pointing to a variable of type card to make this work. If I have to assign everything to a new variable just to pass it to a function, it defeats the convenience of the function. I have a number of 'card' variables that are already in use, and I suddenly need to just swap some values for a variant in the game. It would be a real hassle to not be able to use such a function.
THanks for any help.
rob
typedef struct {int rank; char suit;} card;
I am trying to write a function that will swap the values of 2 variables of type card. My efforts so far look like this:
void SwapCards (card *card1, card *card2)
{
card temp_card;
temp_card = *card1;
*card1 = *card2;
*card2 = temp_card;
}
the problem is that this does not work.
I have tried using it as follows:
card card10, card20;
card10.suit='c';
card10.rank=12;
card20.suit='d';
card20.rank=10;
SwapCards(*card10, *card20);
Please tell me I don't need to declare pointer variables pointing to a variable of type card to make this work. If I have to assign everything to a new variable just to pass it to a function, it defeats the convenience of the function. I have a number of 'card' variables that are already in use, and I suddenly need to just swap some values for a variant in the game. It would be a real hassle to not be able to use such a function.
THanks for any help.
rob