Matrice与人物

时间:2014-07-14 20:26:25

标签: c++ matrix

大家好我尝试打印带有字符的矩阵。我想是这样的:

#include <iostream>
using namespace std;
int main()
{
    char a[3][3]={"a","b","c","d","e","f","g","h","i"};
    int i,j;
    for(i=0;i<3;i++)
        {
            for(j=0;j<3;j++)
            {
            cout<<a[i][j];
            }
        cout<<"\n";
        }
}

我做错了什么?提前谢谢。

4 个答案:

答案 0 :(得分:4)

你有一个3乘3的int数组。您正在尝试使用字符串文字初始化该数组的每个元素。根本没有从字符串文字(在本例中为const char[2])的类型到int的隐式转换。

如果您只想要一个字符矩阵,那么请将数组元素设为char。然后你需要使用带单引号的字符文字,而不是字符串文字。

char a[3][3]={'a','b','c','d','e','f','g','h','i'};

答案 1 :(得分:1)

你可能想要这个:

char a[3][3]={'a','b','c','d','e','f','g','h','i'};

答案 2 :(得分:-1)

改变这个:

int a[3][3]={"a","b","c","d","e","f","g","h","i"};

要:

char a[3][3]={{"a","b","c"},{"d","e","f"},{"g","h","i"}};

答案 3 :(得分:-1)

尝试更改:

int a[3][3]={"a","b","c","d","e","f","g","h","i"};

要:

char a[3][3]={'a','b','c','d','e','f','g','h','i'};

那应该去掉铸造字符到整数。

编辑:将双引号更改为单引号