[英]excess elements in char array initializer
I'm trying to improve my lousy C skills so I'm writing a tic tac toe program. 我正在尝试提高糟糕的C技能,所以我正在编写井字游戏程序。 It's only in its first steps but I would like to be able to print a board before going into the whole algorithms of the game itself.
这只是第一步,但我希望能够在进入游戏本身的整个算法之前先印制电路板。 I'm planning to use 1 for x and 0 for circle in order to be able to use sums later.
我计划对x使用1,对圆使用0,以便以后可以使用总和。 I'm working with gcc (ubuntu) and am getting this error:
我正在使用gcc(ubuntu),并且遇到此错误:
xo.c:11:3: error: (near initialization for ‘board[0]’)
xo.c:11:3: error: excess elements in char array initializer
xo.c:11:3: error: (near initialization for ‘board[0]’)
xo.c:11:3: error: excess elements in char array initializer
xo.c:11:3: error: (near initialization for ‘board[1]’)
xo.c:11:3: error: excess elements in char array initializer
xo.c:11:3: error: (near initialization for ‘board[1]’)
xo.c:11:3: error: excess elements in char array initializer
xo.c:11:3: error: (near initialization for ‘board[2]’)
xo.c:11:3: error: excess elements in char array initializer
xo.c:11:3: error: (near initialization for ‘board[2]’)
I would like to initiate a 2 dimensional array of 3x3 for the board. 我想为该板启动一个3x3的二维数组。 I don't understand why is there an excess, I initialized a [3][3] and entered 9 elements.
我不明白为什么会有多余的内容,因此我初始化了一个[3] [3]并输入了9个元素。 Here is the code I've written:
这是我编写的代码:
#include<stdio.h>
/***** Create two dimensional array full of blank spaces
1 marks the X , 0 marks the circle.
Let use enter coordinates (line and coloumn) for placing the 1 (x) and automatically print the board with the computers' move.
*****/
int i = 0;
int j = 0;
void main() {
char board[3][3] = {
{" " , " " , " "} , {" " , " " , " "} , {" " , " " , "t"}
};
for(i=0 ; i<3 ; i++) {
for(j=0 ; j<3 ; j++) {
printf("%c " , board[i][j]);
}
printf("\n");
}
}
" "
is a string ( char *
) literal. " "
是字符串( char *
)文字。 You want ' '
for a single char
. 你想
' '
单个char
。
Change: 更改:
char board[3][3] = {
{" " , " " , " "} , {" " , " " , " "} , {" " , " " , "t"}
};
to: 至:
char board[3][3] = {
{' ' , ' ' , ' '} , {' ' , ' ' , ' '} , {' ' , ' ' , 't'}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.