我正在使用C ++开发BlackJack游戏,我在其中有以下代码,我收到错误
typedef struct
{
int value;
char suit[8];
char name[8];
}Deck;
Deck Cards[52] = {{ 1,"Ace","Hearts"},{ 2, "Two","Hearts"}, { 3, "Three", "Hearts"}, { 4, "Four","Hearts"}, { 5,"Five","Hearts"},{ 6,"Six", "Hearts06"},
{ 7,"Seven","Hearts"},{ 8,"Eight","Hearts"},{ 9,"Nine","Hearts"},{ 10,"Ten","Hearts"},{10,"Jack","Hearts"},{10,"Queen","Hearts"},{10,"King","Hearts"},
{ 1,"Ace","Clubs"},{2, "Two", "Clubs"},{3,"Three","Clubs"},{4,"Four","Clubs"},{5,"Five","Clubs"},{6,"Six","Clubs"},{7,"Seven","Clubs"},{8,"Eight","Clubs"},
{ 9,"Nine","Clubs"},{10,"Ten","Clubs"},{10,"Jack","Clubs"},{10,"Queen","Clubs"},{10,"King","Clubs"},
{ 1,"Ace","Diamonds"},{2,"Two","Diamonds"},{3,"Three","Diamonds"},{4,"Four","Diamonds"},{5,"Five","Diamonds"},{6,"Six","Diamonds"},{7,"Seven","Diamonds"},
{ 8,"Eight","Diamonds"},{9,"Nine","Diamonds"},{10,"Ten","Diamonds"},{10,"Jack","Diamonds"},{10,"Queen","Diamonds"},{10,"King","Diamonds"},
{ 1,"Ace","Spades"},{ 2,"Two","Spades"},{3,"Three","Spades"},{4,"Four","Spades"},{5,"Five","Spades"},{6,"Six","Spades"},{7,"Seven","Spades"},
{ 8,"Eight","Spades"},{ 9,"Nine","Spades"},{10,"Ten","Spades"},{10,"Jack","Spades"},{10,"Queen","Spades"},{10,"King","Spades"}};
错误是
Main.c:39:127: error: initializer-string for array of chars is too long [-fpermissive]
第39行是上面发布的代码中的最后一行
请帮我弄清楚编译器抛出错误的原因
答案 0 :(得分:15)
字符串"Diamonds"
有9个字符,包括空终止字符。因此,name
必须至少包含9个元素。
但是,您的name
成员应该被称为suit
,反之亦然。
答案 1 :(得分:1)
而不是:
int nMyArray[8]= {5,6,5,4,6,7,4,2};
首选:
int nMyArray[]= {5,6,5,4,6,7,4,2};
初始化数组时。前一个要求您指定大小。后者计算大小(仅在编译时)。