我试着制作绘制 8x8 chesstable.Help 我的 c 数组好吗?

I tried to make c array that draw 8x8 chesstable.Help me please?

#include<stdio.h>
#include<stdlib.h>  

int main()

{
    const char *chess[8][8]; //using 2 dimensional array 

    chess[0][0] = "X";
    chess[0][2] = "X";
    chess[0][4] = "X";   //writing values step by step
    chess[0][6] = "X";


    chess[1][1] = "X";
    chess[1][3] = "X"; //same
    chess[1][5] = "X";
    chess[1][7] = "X";

    chess[2][0] = "X";
    chess[2][2] = "X";// same
    chess[2][4] = "X";
    chess[2][6] = "X";

    chess[3][1] = "X";
    chess[3][3] = "X";
    chess[3][5] = "X";
    chess[3][7] = "X";

    chess[4][0] = "X";
    chess[4][2] = "X";
    chess[4][4] = "X";  // same
    chess[4][6] = "X";

    chess[5][1] = "X";
    chess[5][3] = "X";
    chess[5][5] = "X";  // same
    chess[5][7] = "X";

    chess[6][0] = "X";
    chess[6][2] = "X";
    chess[6][4] = "X";  //same
    chess[6][6] = "X";

    chess[7][1] = "X";
    chess[7][3] = "X";  //same
    chess[7][5] = "X";
    chess[7][7] = "X";

    int i;
    for (i = 0; i < 8; i++) //printing out array
    {
        int j;
        for (j = 0; j < 8; j++)
            printf("%c",  &chess[i][j]); //same
    }
    printf("\n");
    system("pause");  //same
    return 0;  


}

我能发现五个问题:

1) 类型错误:

这个

const char *chess[8][8]; 

是 64 个字符指针,但您可能需要 64 个字符,例如

const char chess[8][8]; 

2) 用字符串而不是字符初始化

这里

chess[0][0] = "X";

您使用字符串进行初始化,但可能需要一个字符,例如

chess[0][0] = 'X'; // single ' instead of double "

3) 你没有初始化整个数组

一些数组元素,例如[0][1],未初始化。因此,当您在循环中打印它们时,您会读取未初始化的值。

查看此答案的末尾,了解更紧凑的电路板初始化方法。

4) 打印参数错误

这里

printf("%c",  &chess[i][j]);

你获取字符的地址。这是错误的 - 简单地做:

printf("%c",  chess[i][j]);

5) 将 const 用于非常量变量

这里

const char *chess[8][8];

你说chess是常数。那么你以后就不能改变它了。相反,你想要

char *chess[8][8];

所以把它们放在一起就是:

#include<stdio.h>
#include<stdlib.h>  

int main()

{
    char chess[8][8]; //using 2 dimensional array 

    for(int g=0; g<8; ++g)
        for (int t=0; t<8; ++t)
            chess[g][t] = 'Z';

    chess[0][0] = 'X';
    chess[0][2] = 'X';
    chess[0][4] = 'X';   //writing values step by step
    chess[0][6] = 'X';


    chess[1][1] = 'X';
    chess[1][3] = 'X'; //same
    chess[1][5] = 'X';
    chess[1][7] = 'X';

    chess[2][0] = 'X';
    chess[2][2] = 'X';// same
    chess[2][4] = 'X';
    chess[2][6] = 'X';

    chess[3][1] = 'X';
    chess[3][3] = 'X';
    chess[3][5] = 'X';
    chess[3][7] = 'X';

    chess[4][0] = 'X';
    chess[4][2] = 'X';
    chess[4][4] = 'X';  // same
    chess[4][6] = 'X';

    chess[5][1] = 'X';
    chess[5][3] = 'X';
    chess[5][5] = 'X';  // same
    chess[5][7] = 'X';

    chess[6][0] = 'X';
    chess[6][2] = 'X';
    chess[6][4] = 'X';  //same
    chess[6][6] = 'X';

    chess[7][1] = 'X';
    chess[7][3] = 'X';  //same
    chess[7][5] = 'X';
    chess[7][7] = 'X';

    int i;
    for (i = 0; i < 8; i++) //printing out array
    {
        int j;
        for (j = 0; j < 8; j++)
            printf("%c",  chess[i][j]); //same
        printf("\n");
    }
    printf("\n");
    //system("pause");  //same
    return 0;  
}

输出:

XZXZXZXZ
ZXZXZXZX
XZXZXZXZ
ZXZXZXZX
XZXZXZXZ
ZXZXZXZX
XZXZXZXZ
ZXZXZXZX

要获得更紧凑的初始化方式,您可以这样做:

#include<stdio.h>
#include<stdlib.h>  

int main()

{
    char chess[8][8]; //using 2 dimensional array 

    for(int g=0; g<8; ++g)
        for (int t=0; t<8; ++t)
        {
            if (t % 2 == g % 2)
                chess[g][t] = 'X';
            else
                chess[g][t] = 'Z';
        }

    int i;
    for (i = 0; i < 8; i++) //printing out array
    {
        int j;
        for (j = 0; j < 8; j++)
            printf("%c",  chess[i][j]); //same
        printf("\n");
    }
    printf("\n");
    //system("pause");  //same
    return 0;  
}