initializer-string for array of chars is too long error

喜夏-厌秋 提交于 2020-05-25 07:26:25

问题


I am working on a BlackJack Game using C++, and I have the following piece of code within it where I am getting an error

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"}};

The error is

Main.c:39:127: error: initializer-string for array of chars is too long [-fpermissive]

Line 39 is the last line in the code posted above

Please help me in figuring out why the compiler is throwing an error


回答1:


The string "Diamonds" has 9 characters including the null terminating character. Therefore, name must have at least 9 elements.

However, it looks like your name member should be called suit and vice versa.




回答2:


Instead of:

int nMyArray[8]= {5,6,5,4,6,7,4,2};

Prefer this:

int nMyArray[]= {5,6,5,4,6,7,4,2};

When you are initializing an array. The former one requires you to specify size. The latter one computes the size (at compile time only).



来源:https://stackoverflow.com/questions/21407898/initializer-string-for-array-of-chars-is-too-long-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!