0

I've got a main.c file which includes all start up stuff. I've initialised the deck, but I am having trouble storing the temporary value of the array into a variable: `

enum suit
{
    CLUB, DIAMOND, HEART, SPADE
};

typedef enum suit Suit;

enum face
{
    ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, 
    QUEEN, KING
};
typedef enum face Face;

struct card
{
    Suit c_suit;
    Face c_face;
};

typedef struct card Card;
void shuffle(Card * deck);

void shuffle(Card * deck)
{
    int temp;
    int y, x, t=0;  
    for (x = 52; x > 0 ; x--)
    { 
        y = rand() % x;   
        temp = deck[x];
        deck[x] = deck[y];
        deck[y] = temp;
    }

}

`

1 Answers1

2

You are assigning the address of deck[x] to int variable.

temp = &deck[x];

You want to do simple swap, so just assign the 'value' of deck[x] to temp, i.e.

temp = deck[x];

UPDATE:
As cyco130 said in his comment, you can enable more compiler warnings using -Wall flag to gcc while compiling.
However AFAIW, this problem should be reported as an error by compiler since you are trying to store the int * value to an int without explicit typecast.

0xF1
  • 6,046
  • 2
  • 27
  • 50
  • +1. I want to add that enabling more warnings would catch this in every major C compiler. Warnings are our friends :) – cyco130 Apr 04 '14 at 06:38
  • I've changed temp = &deck[x]; to: temp = deck[x]; and I am getting an error message of: incompatible types when assigning to type 'Card' from type 'int' vice versa – user3496710 Apr 04 '14 at 06:45
  • Is `deck` an array of 52 `Card`s? If yes, then have you initialized all the array elements with proper `Suit` and `Face`? If yes, then in that case you can simply use `temp` in the same way, with its type as `Card`, i.e. use `Card temp` instead of `int temp`. – 0xF1 Apr 04 '14 at 07:10
  • I tried this and it worked perfectly! I used Card temp in my header file and referenced it in my function! Thank you very very much – user3496710 Apr 04 '14 at 07:46
  • You should accept the answer(clicking the checkmark beside the answer) if it solves your problem completely. :-) – 0xF1 Apr 04 '14 at 07:57