char的初始化程序太多[9] [9]

时间:2012-01-01 19:38:13

标签: c++ arrays

但事实是,我声明的char数组中确实存在大量的初始化器。

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

有9列九列。我的问题是什么?我检查了其他论坛和这个答案,但没有发现任何帮助。

7 个答案:

答案 0 :(得分:14)

您正在使用字符串而不是字符初始化数组,因此每个元素都尝试适合char和null终止符。试试'1','2','3'等等。

答案 1 :(得分:9)

您需要将所有双引号""更改为单引号''

否则,它们是字符串而不是char s。

在这种情况下,一个简单的查找和替换应该可以解决问题。

答案 2 :(得分:2)

从字符串(")更改为字符,请改用单引号(')

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

答案 3 :(得分:1)

改为使用单引号:

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

答案 4 :(得分:0)

如果你使用双引号,就像这样......

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

否则,请使用单引号。请注意,您可以收到警告,例如

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

来自G ++ 4.6.2(编译器选项-Wall -Wextra)。

答案 5 :(得分:0)

您不应该使用单引号'而不是双引号"吗?

答案 6 :(得分:-2)

将char更改为char *并保留双引号。如果要将其用作角色,请将其作为角色重新投射。这样你就可以同时拥有字符和字符数组(字符串)。