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

- 7,173
- 6
- 33
- 61

- 33
- 1
- 8
-
You have two problems. You're using the wrong format specifier (change the `printf` statement to `printf("%s", chess[i][j]);`). Also, you're accessing uninitialized values. Initialize the entire 2-D array to `" "` and then change the specified values to `"X"`. Also, you want `printf("\n");` after the inner `for` loop. – Eli Sadoff Nov 28 '16 at 18:09
-
What's the question? Try to add 'printf("\n");' at the end of outer loop. – leggo Nov 28 '16 at 18:13
-
After using %s i get ·O╠╠╠╠0{╠╠╠╠0{╠╠╠╠0{0{╠╠╠╠0{╠╠╠╠0{╠╠╠╠0{╠╠╠╠╠╠╠╠0{╠╠╠╠0{╠╠╠╠0{╠╠╠╠0{0{╠╠╠╠0{╠╠╠╠0{╠╠╠╠0{╠╠╠╠╠╠╠╠0{ – bgobekli Nov 28 '16 at 18:28
-
@BatuhanGöbekli - yes, because your current code access memory not assigned to you. In other words - your code have undefined behavior – Support Ukraine Nov 28 '16 at 18:32
1 Answers
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.
See the end of this answer for a more compact way of initializing the board.
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;
}

- 42,271
- 4
- 38
- 63
-
I think that (1) and (2) are not errors themselves. OP wants to use a C string, not chars. – Eli Sadoff Nov 28 '16 at 18:13
-
Also (5) is a not a problem as well. `const char *` is the type for a C string. – Eli Sadoff Nov 28 '16 at 18:23
-
@EliSadoff - Well, in principle you could be right. But you don't want to print a chess board using strings. That would be odd. – Support Ukraine Nov 28 '16 at 18:23
-
It seems to me at least that OP set it up to be using strings. While that might not be the best choice, I don't think it's inherently wrong. – Eli Sadoff Nov 28 '16 at 18:24
-
@EliSadoff - Well, to me it does seem that OP wants chars. So under the assumption that OP wants chars, (5) is a problem. – Support Ukraine Nov 28 '16 at 18:30
-
They might well be strings as posted. The Queen's Bishop and King's Bishop might be named `"QB"` and `"KB"` to narrate the moves. Although I see you were acccepted. – Weather Vane Nov 28 '16 at 18:34
-
@WeatherVane - Yes, they could. My answer is assuming that OP wanted `char` based on the way of printing. If OP really wants strings my answer is useless and candidate for delete. – Support Ukraine Nov 28 '16 at 18:42
-
It was a good guess, so my UV as the OP liked it. Of course you would not sensibly work with strings for the play itself, only as information. – Weather Vane Nov 28 '16 at 18:44