我得到了大部分工作,包括隨機化和洗牌,但是當涉及到分配正確的臉部/套裝值時,我無法做到正確。此外,我正在'中止(核心轉儲)',可能是因爲我幾乎不知道我在做什麼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