2017-06-04 162 views
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]); 
} 
+0

d [I] .deck_cards - 你試圖索引到一個數組,但你必須沒有在數組中傳遞。 – OldProgrammer

+1

如果你想在'InitDeck(struct Deck)'中更改'struct Deck D',你必須將它作爲一個指針'InitDeck(struct Deck *)'傳遞。否則函數中的對象D只是main中的對象的副本。 – AdrianRK

+0

將'deck_cards'換成'struct Deck'的任何特定原因? – alk

回答

0

我創建了一個工作版本,故意儘量保持儘可能接近您自己的嘗試。
我評論了我的更改。我還評論了一些但不是全部的可以改進的東西。

#include <stdio.h> 
#include <stdlib.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; 
}; 

// a struct with a single member, 
// code would be easier if the deck were just an array of cards 
struct Deck 
{ 
    // member of type array of "struct card", 
    // using an array instead of only a pointer, 
    // makes sure that the memory for it exists 
    struct card deck_cards[NCARDS]; 
}; 


void PrintCard(struct Deck D, int i); 

// pointer to deck (i.e. call by reference) needed, 
// to have an external effect 
void InitDeck(struct Deck* D); 

int main(void) 
{ 
    // using this requires <stdlib.h> 
    srand(time(NULL)); 

    struct Deck deck; 

    int i; 

    // give address of the deck you want to change, 
    // otherwise the init will only happen to a copy 
    InitDeck(&deck); 
    for (i=0; i<NCARDS; i++) 
    { 
     PrintCard(deck,i); 
    } 
    return 0; 
} 

void InitDeck(struct Deck* D) 
{ 
    int i; 
    for(i=0; i< NCARDS; i++) 
    { // only for the initialisation of a variable 
     // (a local variable in this case) 
     // an "anonymous struct" similar to what you tried 
     // can be used; but actually it is only a specific syntax 
     // for initialising a struct 
     struct card c = {suit[i/13], face[i%13]}; 
     // D is a pointer to a deck, dereference it by "D->", 
     // which also includes the "." for accessing a member, 
     // the only member "deck_cards", 
     // which is an array, 
     // index into it to access a single card, using "[i]" 
     D->deck_cards[i] = c; 
    } 
} 

void PrintCard(struct Deck deck, int i) 
{ 
    // printing a struct with two pointers to string 
    // requires some more details on how to do that 
    printf("Card %d = %s of %s \n", i, deck.deck_cards[i].f, deck.deck_cards[i].s); 
} 

輸出(編譯gcc -Wall):沒有錯誤,沒有警告

輸出運行:

Card 0 = ace of spades 
Card 1 = two of spades 
Card 2 = three of spades 
Card 3 = four of spades 
Card 4 = five of spades 
Card 5 = six of spades 
Card 6 = seven of spades 
Card 7 = eight of spades 
Card 8 = nine of spades 
Card 9 = ten of spades 
Card 10 = jack of spades 
Card 11 = queen of spades 
Card 12 = king of spades 
Card 13 = ace of hearts 
Card 14 = two of hearts 
Card 15 = three of hearts 
Card 16 = four of hearts 
Card 17 = five of hearts 
Card 18 = six of hearts 
Card 19 = seven of hearts 
Card 20 = eight of hearts 
Card 21 = nine of hearts 
Card 22 = ten of hearts 
Card 23 = jack of hearts 
Card 24 = queen of hearts 
Card 25 = king of hearts 
Card 26 = ace of clubs 
Card 27 = two of clubs 
Card 28 = three of clubs 
Card 29 = four of clubs 
Card 30 = five of clubs 
Card 31 = six of clubs 
Card 32 = seven of clubs 
Card 33 = eight of clubs 
Card 34 = nine of clubs 
Card 35 = ten of clubs 
Card 36 = jack of clubs 
Card 37 = queen of clubs 
Card 38 = king of clubs 
Card 39 = ace of diamonds 
Card 40 = two of diamonds 
Card 41 = three of diamonds 
Card 42 = four of diamonds 
Card 43 = five of diamonds 
Card 44 = six of diamonds 
Card 45 = seven of diamonds 
Card 46 = eight of diamonds 
Card 47 = nine of diamonds 
Card 48 = ten of diamonds 
Card 49 = jack of diamonds 
Card 50 = queen of diamonds 
Card 51 = king of diamonds 
+0

謝謝@Yunnosh – Benq