【发布时间】:2016-06-07 11:50:04
【问题描述】:
我正在处理数组中的卡片类型结构。
struct card deck[DECKSIZE]; //The deck is an array of cards structures
我使用的是二维数组。卡片类型结构数组的数组
struct card allHands[hands][cards];
我使用这个函数将卡片组和数组作为带有数组参数的指针传递。我还更改了牌组指针的位置,以模拟牌组在传递给玩家时丢失卡片。
void dealHands(struct card *deck, struct card **handArray, int hands, int cards){
int players;
int cardCount;
int passCard = 0;
struct card * thisDeck;
thisDeck = deck;
for(players = 0; players < hands; players++){
for(cardCount = 0; cardCount < cards; cardCount++){
handArray[players][cardCount] = thisDeck[passCard];
passCard++;
}
}
deck = (deck + passCard);
}
我用 c 编程已经很久了,所以我想这就是你做原型的方式吗?
void dealHands(struct card[], struct card*[], int, int);
这就像我如何实现该功能的主要框架。
int main(int argc, char *argv[])
{
/* Declare and initialize variables */
int hands = atoi(argv[HANDSINPUT]); //How many players
int cards = atoi(argv[CARDSINPUT]); //How many cards per hand
struct card deck[DECKSIZE]; //The deck is an array of cards structures
struct card allHands[hands][cards];
//Builds the deck
//Shuffles deck with a RNG and swapping every card
int players;
int cardCount;
int passCard = 0;
dealHands(deck, allHands, hands, cards);
}
我在编译过程中得到以下 2 个语句
警告:从不兼容的指针类型传递“dealHands”的参数 2 [默认启用] dealHands(deck, allHands, Hands, Cards); ^
注意:预期为“struct card **”,但参数类型为“struct card ()[(sizetype)(cards)]” void dealHands(struct card[], struct card[], int, int); ^
当我需要在函数中调用指针和数组时,我总是搞砸。所以我不确定我的逻辑在哪里有缺陷。我在哪里传递地址而不是值,反之亦然?
【问题讨论】:
-
struct card **无法获取二维数组。指针不是数组!正如警告所暗示的那样,对参数使用正确的声明。 -
@Olaf 就是这样。我不记得如何正确声明它。如果它的 struct card *handArray[] 或 struct card handArray[][] 或其他东西。我似乎找不到它:/
-
你注意到我发布了一个相当全面的答案,不是吗?
-
不,对不起,我的错。 @Olaf 我还不习惯这个界面。
-
请拨打tour。与往常一样,您应该知道如何使用 SO。
标签: c pointers multidimensional-array struct