People are complaining that you're not using containers and not declaring the size of your array. Don't worry about that, that's not the problem. Someone also said you're going past array boundaries, which you aren't. It's okay to have arrays with size not declared. It's also okay to have an array of pointers to Card. But the thing I don't get is why it crashes. Here's some sample code I wrote, based on your code:
#include <stdio.h>
#include <stdlib.h>
#define DECK_SIZE 24
void shuffle(int deck[]) {
int n = DECK_SIZE, t;
while (n > 1) {
long k = lrand48() % DECK_SIZE;
n--;
t = deck[n];
deck[n] = deck[k];
deck[k] = t;
}
}
int main(int argc, char **argv) {
int deck[DECK_SIZE], i;
for (i = 0; i < DECK_SIZE; ++i)
deck[i] = i + 1;
shuffle(deck);
for (i = 0; i < DECK_SIZE; ++i)
printf("%i\n", deck[i]);
return 0;
}
Run it, it works perfectly fine. That means there is something else going on. Try printing the value of all the cards in your deck before you call shuffle to see if it segfaults there too, I suspect it would.
However, there IS an error in your code. Your function does not shuffle correctly. The correct way to shuffle is not to swap each card with a card selected from the entire deck, but to swap each card at position N with an card selected from the range 0..N. If you swapped each card with a random card you get N^N possible outcomes, some of which overlap if you swap a card back to its original place. With a 3 card deck it's apparent that this is wrong because you will end up with 27 different shuffles, some of which are the same, even though there are 3!=6 permutations of 3 cards. The problem is that since 6 is not a factor of 27, some permutations are more likely than others. To avoid this, try doing it this way:
void shuffle_correctly(int deck[]) {
int i, t, k;
for (i = 2; i < DECK_SIZE; ++i) {
k = lrand48() % i;
t = deck[i-1];
deck[i-1] = deck[k];
deck[k] = t;
}
}