Initializer string is too long

Whenever I attempt to compile this code I get the error "initializer-string for array of chars is too long". Any help is greatly appreciated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <windows.h>

using namespace std;

char Map[10][10] = {"##########",
                    "#        #",
                    "#        #",
                    "#        #",
                    "#        #",
                    "#        #",
                    "#        #",
                    "#        #",
                    "#@       #",
                    "##########" };

int Gamespeed = 100;
int Level = 0;
bool stopgame = false;

int main()
{
    
    while(stopgame == false)
    {
                   
        for (int y = 0; y < 10; y++) 
        {
            cout << Map[y] << endl;
        }
        for (int y = 0; y < 10; y++)
        {
            for (int x = 0; x < 10; x++)
            {
                switch (Map[y][x])
                {
                       case '@':
                       {
                            if (GetAsyncKeyState(VK_UP) != 0)
                            {
                               int y2 = y-1;
                               
                               switch(Map[y2][x])
                               {
                                   case ' ':
                                   {
                                        Map[y][x] = ' ';
                                        y -= 1;
                                        Map[y2][x] = '@';
                                   }
                               }
                            }
                       }
                }
            }
        }
    }
    
}
Each string literal has additional character - terminating zero. So your array shall be declared as

char Map[10][11];

because it is initialized by string literal "##########" that has in total 11 characters (including the terminating zero).
Last edited on
"##########" doesn't contain 10 characters. It contains 11.

There's a hidden '\0' character added to the end, so it actually looks like this:
{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '\0'}

'\0' is called the NUL character, and it's used as a hint where the array ends.
Thank you for the help vlad and Catfish
Last edited on
Topic archived. No new replies allowed.