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