2017-06-04 163 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