1

我在初始化二维字符数组时遇到了困难。当它起作用时,它会给我一个六进制输出。我正在寻找沿网格线的东西。

#include <iostream>


using namespace std;


main() 
{   


//char test[5][5] = {'***\0','***\0','***\0','***\0','***\0'};
char test[5][5] = {"***\0","***\0","***\0","***\0","***\0"};
cout << test << endl;
cout << char[2][2] << endl;


cin.get();
return 0;


}

建议使用字符串数组,但我需要为另一个将其绑定到价格的数组定义数组地址。

4

1 回答 1

0
for(int i = 0; i < 5; i++) {
    for(int j = 0; j < 5; j++) {
        test[i][j] = '*';
    }
}

//Alternatively you can go for this

char test[5][5] = {'*', '*', '*', '*', '*', 
                   '*', '*', '*', '*', '*', 
                   '*', '*', '*', '*', '*', 
                   '*', '*', '*', '*', '*', 
                   '*', '*', '*', '*', '*'};

这显然不会让你准确地得到你想要的东西,它需要一些调整。这将为您提供两个 d 字符数组,全部设置为“*”。尽管使用 std::vector 或 std::array 可能更好,但这至少会起作用。

我认为第二种方法是“危险的”,因为它利用了内存中 test[25] == test[5][5] 的事实,但你应该清楚地使用你更喜欢的那个。

于 2013-05-16T20:34:12.080 回答