Я работаю над BlackJack Game с использованием C ++, и у меня есть следующий фрагмент кода, где я получаю сообщение об ошибке
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 последняя строка в приведенном выше коде
Пожалуйста, помогите мне выяснить, почему компилятор выдает ошибку
Строка "Diamonds"
имеет 9 символов, включая нулевой завершающий символ. Следовательно, name
должно иметь не менее 9 элементов.
Тем не менее, похоже, что ваш name
член должен быть назван suit
и наоборот.
Вместо:
int nMyArray[8]= {5,6,5,4,6,7,4,2};
Предпочитаю это:
int nMyArray[]= {5,6,5,4,6,7,4,2};
Когда вы инициализируете массив. Первый требует от вас указать размер. Последний вычисляет размер (только во время компиляции).