不兼容的指针类型错误

时间:2016-04-14 23:11:41

标签: c

我正在使用枚举,结构和字符串的项目,并且我不断收到不兼容的指针类型错误。我相信这是因为我的枚举是一个整数类型而我的字符串是一个字符,但我无法弄清楚如何修复它。请帮忙!

#include <stdio.h>
#include <string.h>

typedef enum {Clubs,Heats,Diamonds,Spades}Suits_Of_Cards_ENUM;
typedef enum {Ace,Deuce,three,four,five,six,seven,eight,nine,ten,Jack,Queen,King}Rank_Of_Cards_ENUM;

typedef struct
{

Suits_Of_Cards_ENUM suit[4];
Rank_Of_Cards_ENUM rank[13];

}CARDS;


int main(void)
{
    //Declaring Strings
    const char* suit_name[] = {"  Clubs", "  Hearts", "Diamonds", "Spades"};
    const char* rank_name[] = {"Ace", "Deuce", "  3", "  4", "  5", "  6", "  7", "  8", "  9", " 10",
                           "Jack", "Queen", "King"};

    CARDS deck[52];

    int i;
    int count = 0;
    int num = 1;

    for(i=0; i<=52; i++)
    {
        strcpy(deck[i].suit, suit_name[count]);   //Error is here

        //Assigning the card number
        strcpy(deck[i].rank, rank_name[num - 1]);  //Error is here

        num++;

        //If statement for assigning numbers
        if((i+1)%13==0)
        {
             count++;
             num = 1;
        }//end if

    }//end for

    printf("Before Shuffling:\n\n");

    count = 0;
    for(i=0; i<=52; i++)
    {
        printf("%s %s \t", deck[i].rank, deck[i].suit);  //Error is Here
        if(count < 3)
            count++;
        else
        {
            printf("\n");
            count = 0;
        }//end else
    }//end for
    return 0;
}//end main

2 个答案:

答案 0 :(得分:0)

我认为有一个段错误......

CARDS deck[52];

for(i=0; i<=52; i++)
{
    strcpy(deck[i].suit, suit_name[count]);   //Error is here

    //Assigning the card number
    strcpy(deck[i].rank, rank_name[num - 1]);  //Error is here

0&lt; = i&lt; = 52所以我有53个可能的值,如果i = 52,则deck [i]会溢出......

答案 1 :(得分:0)

如果要使用字符串,则需要使用字符串。正如三十二上校所说,你不能strcpy一个字符串进入枚举。此外,我会为一张卡片定义一个struct,然后创建一个阵列来组成你的牌组。像这样:

#include <stdio.h>
#include <string.h>

#define NUM_CARDS_IN_DECK 52

typedef enum {Clubs=0,Heats,Diamonds,Spades,NUM_SUITS}Suits_Of_Cards_ENUM;
typedef enum {Ace=0,Deuce,three,four,five,six,seven,eight,nine,ten,Jack,Queen,King,NUM_RANKS}Rank_Of_Cards_ENUM;

 struct Card
 {

   Suits_Of_Cards_ENUM suitEnum;
   char strSuit[16];

   Rank_Of_Cards_ENUM rankEnum;
   char strRank[16];

 };

 int main(void)
 {
   int cardIndex;
   struct Card deck[NUM_CARDS_IN_DECK];

   //Declaring Strings
   const char* suit_name[] = {"  Clubs", "  Hearts", "Diamonds", "Spades"};
   const char* rank_name[] = {"Ace", "Deuce", "  3", "  4", "  5", "  6", "  7", "  8", "  9", " 10",
                       "Jack", "Queen", "King"};

   for (cardIndex=0; cardIndex<NUM_CARDS_IN_DECK; cardIndex++)
   {
     deck[cardIndex].suitEnum = (Suits_Of_Cards_ENUM)(cardIndex%(int)NUM_SUITS);
     deck[cardIndex].rankEnum = (Rank_Of_Cards_ENUM)(cardIndex%(int)NUM_RANKS);
     strcpy(deck[cardIndex].strSuit, suit_name[cardIndex%(int)NUM_SUITS]);
     strcpy(deck[cardIndex].strRank, rank_name[cardIndex%(int)NUM_RANKS]);
   }

   printf("Before Shuffling:\n\n");

   for(cardIndex=0; cardIndex<NUM_CARDS_IN_DECK; cardIndex++)
   {
     printf("%s %s\n", deck[cardIndex].strRank, deck[cardIndex].strSuit);  //Error is Here
   }//end for
   return 0;
 }

另一种方法是简单地编写函数将enum转换为字符串,而不是在卡片中包含字符串struct