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

时间:2022-09-06 18:42:13

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 *'分配类型'卡'时不兼容的类型”。

As I understand it, this is occurring because the way that I'm accessing the Card inside the struct is incorrect. 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. When I try to use the brackets to index playingCards the compiler dereferences playingCards and accesses the underlying Card data. 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. Then, I put the card at the back of the deck.

我认为这个问题的独特之处在于我正在尝试将struct Hand中的每个指针分配给内存中的某个位置。当我尝试使用括号来索引playCards时,编译器取消引用playCards并访问基础Card数据。我想要做的是循环手牌大小(5)并通过将玩家的牌指针指向我刚刚出局的牌来向每个玩家发牌。然后,我把卡放在甲板的后面。

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. What I'd like to do is something like:

但是,这只会将cardRemoved分配给第一个Card指针。我想做的是:

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 *'

但是当我这样做时,它会解释上面提到的playCards,并抛出错误错误:从类型'struct Card *'分配类型'Card'时出现不兼容的类型

Please let me know what I'm doing wrong here. Any help would be greatly appreciated!

请让我知道我在这里做错了什么。任何帮助将不胜感激!

1 个解决方案

#1


1  

If I'm understanding correctly, what you want is for playingCards to be an array of pointers.

如果我理解正确,你想要的是playCards是一个指针数组。

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:

如果你对playCards的大小没有限制,你可以将playCards作为卡**,如下所示:

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,所以我认为第一个例子就足够了。

#1


1  

If I'm understanding correctly, what you want is for playingCards to be an array of pointers.

如果我理解正确,你想要的是playCards是一个指针数组。

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:

如果你对playCards的大小没有限制,你可以将playCards作为卡**,如下所示:

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,所以我认为第一个例子就足够了。