struct数组的结构

时间:2017-06-04 21:22:43

标签: c arrays pointers struct

我正在尝试使用两个结构创建一副牌,一个用于卡片,另一个用于卡片。我在初始化套牌时遇到错误。当前错误是

"错误:下标值既不是数组也不是指针,也不是向量"

我附上了我的代码。我不知道到底哪里出错了。以上错误适用于该行 " 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]);
}

1 个答案:

答案 0 :(得分: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