Batuhan Göbekli Batuhan Göbekli - 5 days ago 5
C Question

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;


}

Answer

I can spot five problems:

1) Wrong type:

This

const char *chess[8][8]; 

is 64 char-pointers but you probably want 64 chars, like

const char chess[8][8]; 

2) Initializing with string instead of char

Here

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

you use string for initialization but probably want a char, like

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

3) You don't initialize the full array

Some of the array elements, e.g. [0][1], are uninitialized. So when you print them in the loop, you read the uninitialized value.

4) Wrong argument for printing

Here

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

you take the address of the char. That is wrong - simply do:

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

5) Use of const for a non-const variable

Here

const char *chess[8][8];

you say that chess is a constant. Then you can't change it later on. Instead you want

char *chess[8][8];

So putting it all together would be:

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

Output:

XZXZXZXZ
ZXZXZXZX
XZXZXZXZ
ZXZXZXZX
XZXZXZXZ
ZXZXZXZX
XZXZXZXZ
ZXZXZXZX

For a more compact way of initializing, you could do:

#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;  
}
Comments