0
我试图用两个结构创建一副牌,一个用于卡,另一个用于卡组。我在初始化卡组时遇到错误。电流误差是结构数组的结构
“错误:下标值既不是数组也不指针也不载体”
我重视我的代码。我不知道我到底在哪里错了。上面的错误是针对行 “D [i] .deck_cards =(c.s [i/13] = suit [i/13],c.f [i%13] = face [i%13]);”
如果有更好的方法,请给我建议。感谢您的帮助。
#include <stdio.h>
#include<time.h>
#define NCARDS 52
char* suit[4] = {"spades","hearts","clubs","diamonds"};
char* face[13] ={"ace","two","three","four","five","six","seven","eight","nine",
"ten","jack","queen","king"};
struct card{
char* s;
char* f;
};
struct Deck{
struct card* deck_cards;
};
void PrintCard(struct Deck, int i);
void InitDeck(struct Deck);
int main()
{
srand(time(NULL));
struct Deck deck;
int i;
InitDeck(deck);
for (i=0; i<NCARDS; i++)
{
PrintCard(deck,i);
}
return 0;
}
void InitDeck(struct Deck D)
{
int i;
struct card c;
for(i=0; i< NCARDS; i++)
{
D[i].deck_cards = (c.s[i/13] = suit[i/13] , c.f[i%13] = face[i%13]);
}
}
void PrintCard(struct Deck, int i)
{
printf("Card %d = %s \n", i, Deck.deck_cards[i]);
}
d [I] .deck_cards - 你试图索引到一个数组,但你必须没有在数组中传递。 – OldProgrammer
如果你想在'InitDeck(struct Deck)'中更改'struct Deck D',你必须将它作为一个指针'InitDeck(struct Deck *)'传递。否则函数中的对象D只是main中的对象的副本。 – AdrianRK
将'deck_cards'换成'struct Deck'的任何特定原因? – alk