2015-09-30 112 views
1

我試圖提高我糟糕的C技巧,所以我正在寫一個tic tac toe程序。這只是第一步,但我希望在進入遊戲本身的所有算法之前能夠打印棋盤。我打算將1用於x和0用於圈,以便稍後可以使用總和。 我與海灣合作委員會(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]’) 

我想發起3×3的2維陣列,用於電路板。我不明白爲什麼會有過剩,我初始化了[3] [3]並輸入了9個元素。這是我寫的代碼:

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

回答

4

" "是一個字符串(char *)文字。您想爲char單獨購買' '

變化:

char board[3][3] = { 
     {" " , " " , " "} , {" " , " " , " "} , {" " , " " , "t"} 
    }; 

到:

char board[3][3] = { 
     {' ' , ' ' , ' '} , {' ' , ' ' , ' '} , {' ' , ' ' , 't'} 
    };