简体   繁体   English

如何在C的结构内索引指针

[英]How to Index a Pointer Inside a Struct in C

How do I index a pointer of variable length inside a struct? 如何在结构内索引可变长度的指针? My situation is as follows: 我的情况如下:

I am working on a card simulation game and I am trying to point the playing cards to the same location as a particular card in the deck. 我正在玩纸牌模拟游戏,我试图将纸牌指向与卡组中特定纸牌相同的位置。 However, I am getting the error, "error: incompatible types when assigning to type 'Card' from type 'struct Card *'". 但是,我收到错误消息:“错误:从'struct Card *'类型分配给'Card'类型时,类型不兼容”。

As I understand it, this is occurring because the way that I'm accessing the Card inside the struct is incorrect. 据我了解,这是因为我在结构内部访问Card的方式不正确。 That is, I believe I set up the Hand type properly, but I'm not indexing the cards inside of it correctly to assign the pointers to the same location as the deck's cards. 就是说,我相信我正确设置了Hand类型,但是我没有正确索引其中的卡以将指针分配到与牌组卡相同的位置。

What I think makes this question unique is that I'm trying to assign each pointer in the struct Hand to a location in memory. 我认为使这个问题与众不同的是,我试图将struct Hand中的每个指针分配给内存中的某个位置。 When I try to use the brackets to index playingCards the compiler dereferences playingCards and accesses the underlying Card data. 当我尝试使用方括号来索引playingCards时,编译器将取消引用playingCards并访问基础的Card数据。 What I'd like to do is loop over the hand size (5) and deal a card to each player by pointing the player's card pointer to the card I just dequeued. 我想做的是遍历手的大小(5),然后通过将玩家的纸牌指针指向刚出队的纸牌来向每位玩家分发一张纸牌。 Then, I put the card at the back of the deck. 然后,我将卡片放在卡座的背面。

My data abstractions are as follows and my code to assign the pointer is as follows: 我的数据抽象如下,我分配指针的代码如下:

typedef struct card_{
    char* rank;
    char* suit;
} Card;

typedef struct deck_{
    Queue cardDeck;
}Deck;

typedef struct hand_{
    Card *playingCards;
}Hand;

typedef struct player_{
    Hand hand;
    int bank_roll;
}Player;

typedef struct table_{
    Player player[NUM_PLAYERS];
}Table;

Pointer assignment: 指针分配:

cTable->player[playerIndex].hand.playingCards = cardRemoved;

However, this only assigns cardRemoved to the first Card pointer. 但是,这仅将cardRemoved分配给第一个Card指针。 What I'd like to do is something like: 我想做的是这样的:

cTable->player[playerIndex].hand.playingCards[cardIndex] = cardRemoved;

But then when I do that it dereferences playingCards as mentioned above, and throws the error error: incompatible types when assigning to type 'Card' from type 'struct Card *' 但是,当我这样做时,它如上所述就取消了对playingCards的引用,并引发错误error: incompatible types when assigning to type 'Card' from type 'struct Card *'

Please let me know what I'm doing wrong here. 请让我知道我在做什么错。 Any help would be greatly appreciated! 任何帮助将不胜感激!

If I'm understanding correctly, what you want is for playingCards to be an array of pointers. 如果我理解正确,那么您想要的是playingCards是一个指针数组。

If you know the size of the array you can just make it a static array like you did for the Table struct, as follows: 如果知道数组的大小,则可以像对Table结构那样将其设置为静态数组,如下所示:

typedef struct hand_{
    Card *playingCards[NUM_CARDS_IN_HAND];
}Hand;

this makes playingCards an array of Card *, and the code you mentioned above 这使playCards成为Card *的数组,以及您上面提到的代码

cTable->player[playerIndex].hand.playingCards[cardIndex] = cardRemoved;

would work. 会工作。

If you don't have a limit on the size of playingCards, you can make playingCards a Card **, as follows: 如果您对纸牌的大小没有限制,可以将纸牌**变成纸牌**,如下所示:

typedef struct hand_{
    Card **playingCards;
}Hand;

and then dynamically allocate the space as you add and remove cards to it. 然后在您向其添加和移除卡时动态分配空间。

In this case, you know that the max number of cards in a hand will be 5, so I think the first example would be sufficient. 在这种情况下,您知道一手牌的最大张数为5,因此我认为第一个示例就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM