我得到了大部分工作,包括随机化和洗牌,但是当涉及到分配正确的脸部/套装值时,我无法做到正确。此外,我正在'中止(核心转储)',可能是因为我几乎不知道我在做什么malloc
(如果有的话,在这种情况下)。纸牌游戏问题 - 记忆和奇数值
typedef struct cards {
char suits[4][9], faces[13][6];
int suit, face, card;
} cards;
const int SHOE_SIZE = DECK_SIZE * numberOfDecks; // user given input, please disregard
cards shoe[SHOE_SIZE];
init_struct(&shoe);
cards *shoe_p = malloc(sizeof(cards) + 1000 * sizeof(int));
shoe_p = shoe;
int i;
for (i = 0; i < SHOE_SIZE; i++) {
shoe[i].card = i;
shoe[i].suit = shoe[i].card % 4; // maybe one of these should be % and another/
shoe[i].face = shoe[i].card % 13; // but when I try that, I get strings like "ace of ace"
printf("card #%d = %s of %s\n", i+1, shoe->faces[shoe[i].face], shoe->suits[shoe[i].suit]);
}
free(shoe);
我遗漏了的部分代码怀疑是所描述问题的来源。请让我知道我是否应该提供更多信息!
编辑:其他问题;我是否以适当的方式访问了我的结构成员'面孔'和'适合'?对我来说似乎是这样,但是再次,我看不到还有什么会导致我的字符串的奇怪输出(请参阅代码中的评论)。
另外,我可以将SHOE_SIZE作为我的数组的成员,并以相同的方式(鞋 - >变量)访问它,而不必先通过变量SHOE_SIZE分配它?
如何使用malloc创建鞋? – kensing 2013-05-12 16:12:20
@kensing'shoe = malloc(sizeof(cards)* SHOE_SIZE);' – 2013-05-12 16:12:56
(+1)for“mallocated” – 2013-05-12 16:13:15