Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 结构数组的结构_C_Arrays_Pointers_Struct - Fatal编程技术网

C 结构数组的结构

C 结构数组的结构,c,arrays,pointers,struct,C,Arrays,Pointers,Struct,我正在尝试使用两个结构创建一组卡片,一个用于卡片,另一个用于卡片。我在初始化数据组时出错。当前错误为 “错误:下标值既不是数组,也不是指针或向量” 我已附上我的代码。我不知道我到底错在哪里。上述错误适用于该行 “D[i].卡牌=(c.s[i/13]=西装[i/13],c.f[i%13]=面[i%13]) 如果有更好的方法,请告诉我。谢谢你的帮助 #include <stdio.h> #include<time.h> #define NCARDS 52 char* sui

我正在尝试使用两个结构创建一组卡片,一个用于卡片,另一个用于卡片。我在初始化数据组时出错。当前错误为

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

我已附上我的代码。我不知道我到底错在哪里。上述错误适用于该行 “D[i].卡牌=(c.s[i/13]=西装[i/13],c.f[i%13]=面[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]);
}
#包括
#包括
#定义NCARDS 52
char*suit[4]={“黑桃”、“红桃”、“梅花”、“钻石”};
char*face[13]={“A”、“二”、“三”、“四”、“五”、“六”、“七”、“八”、“九”,
“十”、“杰克”、“女王”、“国王”};
结构卡{
char*s;
char*f;
};
结构甲板{
结构卡*甲板卡;
};
无效打印卡(结构组,int i);
空甲板(结构甲板);
int main()
{
srand(时间(空));
结构甲板;
int i;
甲板(甲板);

对于(i=0;i我创建了一个工作版本,有意尝试尽可能接近您自己的尝试。
我评论了我的改变。 我也评论了一些,但不是所有,可以改进的事情

#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);
}

D[i].deck_cards-您正在尝试索引到数组中,但尚未传入数组。如果要更改
InitDeck(struct deck)
中的
struct deck D
,则必须将其作为指针
InitDeck(struct deck*)传递
。否则,函数中的对象D只是main中对象的副本。您将
卡片组
包装到
结构卡片组
中有什么特殊原因吗?谢谢@Yunnosh
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