Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

initializer-string for array of chars is too long error

Tags:

c++

blackjack

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

like image 508
Praveen Gowda I V Avatar asked Jan 28 '14 14:01

Praveen Gowda I V


2 Answers

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.

like image 65
Joseph Mansfield Avatar answered Sep 21 '22 12:09

Joseph Mansfield


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).

like image 34
Ajay Avatar answered Sep 19 '22 12:09

Ajay