【发布时间】:2021-05-19 14:36:54
【问题描述】:
typedef struct {
char* value;
char* type;
} CARD;
CARD cards[52];
void initializeCards() {
char* types[10] = {"Spade", "Club", "Hearts", "Diamonds"};
char* values[13] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "B", "D", "K"};
shuffleArray(&values, 4); //I've also tried *values, or just values
for (int i = 0; i < 4; i++) {
for (int e = 0; e < 13; e++) {
cards[i*13 + e].type = types[i];
cards[i*13 + e].value = values[e];
}
}
}//initializeCards.
//Code in this function comes from this thread: https://stackoverflow.com/questions/34844003/changing-array-inside-function-in-c
//I've tried many different things inside this function, none of these seem to work.
void shuffleArray(char** array, uint8_t size) {
free(*array);
*array = malloc(size * sizeof(int));
//char* temp = (*array)[0];
//(*array)[0] = 1;
(*array)[0] = "test";
printf("%s\n", &array[0]);
}//shuffleArray.
我正在尝试将一副纸牌放入一个数组中,这些纸牌应该被洗牌,但我不想用纸牌洗牌数组,我想洗牌这两个较短的数组“值”和“类型”,因为它们更短,因此洗牌的迭代次数更少。
我尝试了很多不同的方法,但都没有奏效,我想要的是更改数组的指针 在函数 'shuffleArray' 中,以便其他函数中的数组也会自动更改。
如果你们中的任何人可以帮助我正确地执行以下操作,我想我会设法完全改组它们:
//This is pseudo-code, if I get this correctly I should be able to shuffle them.
temp = array[0]
array[0] = array[size-1]
array[size-1] = temp
【问题讨论】: