9

But the thing is, there are exactly the amount of initializers in the char array that I declared.

char dash[9][9]={
        {"1","2","3","4","5","6","7","8","9"},
        {"a","b","c","d","e","f","g","h","i"},
        {"q","w","e","r","t","y","u","i","o"},
        {"9","8","7","6","5","4","3","2","1"},
        {"i","h","g","f","e","d","c","b","a"},
        {"o","i","u","y","t","r","e","w","q"},
        {"z","x","y","w","v","u","t","s","r"},
        {"a","l","l","s","t","a","r","p","y"},
        {"m","o","n","d","o","l","o","r","i"}
    };

There are nine rows of nine columns. What's my problem? I checked other forums and this one for answers but found nothing that helped.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

7 Answers7

14

You're initializing the array with strings, not chars, thus each element is trying to fit in the char and a null terminator. Try '1', '2', '3', etc.

Nick Shaw
  • 2,083
  • 19
  • 27
9

You need to change all your double-quotes "" to single quotes ''.

Otherwise, they are strings instead of chars.

In this case, a simple find-and-replace should do the trick.

Mysticial
  • 464,885
  • 45
  • 335
  • 332
2

Change from strings (") to chars, Use single-quotes (') instead.

char dash[9][9]={
        {'1','2','3','4','5','6','7','8','9'},
        {'a','b','c','d','e','f','g','h','i'},
        {'q','w','e','r','t','y','u','i','o'},
        {'9','8','7','6','5','4','3','2','1'},
        {'i','h','g','f','e','d','c','b','a'},
        {'o','i','u','y','t','r','e','w','q'},
        {'z','x','y','w','v','u','t','s','r'},
        {'a','l','l','s','t','a','r','p','y'},
        {'m','o','n','d','o','l','o','r','i'}
    };
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
1

Use single quote instead:

char dash[9][9]={
    {'1','2','3','4','5','6','7','8','9'},
    {'a','b','c','d','e','f','g','h','i'},
    {'q','w','e','r','t','y','u','i','o'},
    {'9','8','7','6','5','4','3','2','1'},
    {'i','h','g','f','e','d','c','b','a'},
    {'o','i','u','y','t','r','e','w','q'},
    {'z','x','y','w','v','u','t','s','r'},
    {'a','l','l','s','t','a','r','p','y'},
    {'m','o','n','d','o','l','o','r','i'}
};
ScrollerBlaster
  • 1,578
  • 2
  • 17
  • 21
0

If you use double quotes, it is done like this...

char dash[9][9] =
{
    "123456789",
    "abcdefghi",
    "qwertyuio",
    "987654321",
    "ihgfedcba",
    "oiuytrewq",
    "zxywvutsr",
    "allstarpy",
    "mondolori"
};

Otherwise, use single quotation marks. Note that you can get a warning such as

error: initializer-string for array of chars is too long [-fpermissive]

from G++ 4.6.2 (compiler options -Wall -Wextra).

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • thank you Johnathon, i should not have tried to shortcut my answer. – Gerald P. Wright Jan 01 '12 at 19:48
  • 2
    C permits a `char[9]` array to be initialized with a string literal of length exactly 9; the resulting array is not null-terminated (i.e., it's not a string). C++ doesn't permit this, and a compiler other than G++ could well reject the code altogether. (Also, you need commas between the string literals; otherwise, you effectively have a single 81-character literal. I'll go ahead and add the commas.) – Keith Thompson Jan 08 '12 at 11:54
0

Shouldn't you use single quotes ' instead of double quotes "?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Jaroslaw Stadnicki
  • 1,178
  • 1
  • 8
  • 17
-2

Change the char to char* and leave the double quotes. When you want to use it as character, cast it back as a character. This way you can have both characters and arrays of characters (strings).

Nicolas Epaminonda
  • 181
  • 1
  • 3
  • 13
  • 1
    Interesting idea - change the declarator instead of the initializer. Surely though, you wouldn't cast anything when you wanted a `char`; you'd dereference the `char *` in the array: `*dash[i][j]` or even `dash[i][j][0]`? This solution would dramatically increase the storage required for the array, from 81 bytes to '81 pointers plus 81 strings of 2 bytes each' (either 6 or 10 times as much storage, depending on whether you have a 32-bit or 64-bit system). – Jonathan Leffler Jan 01 '12 at 20:32
  • Yes, I've only made that suggestion to show that there's another way of manipulating arrays. But of course, I do agree that there's an extra storage cost when introducing pointers. – Nicolas Epaminonda Jan 01 '12 at 23:21
  • Casting a `char*` to a `char` will give you some implementation-defined value, likely the low-order byte of the memory address. How is that useful? – Keith Thompson Jan 08 '12 at 11:51