2014-03-29 36 views
0

對於數組而言,我一直都會遇到下述錯誤:在selectLocation函數中,下標值既不是指針也不是矢量。這只是什麼是一個井字棋計劃的開始。我究竟做錯了什麼?既不是參數,也不是指針

void selectLocation(char board[],int choice); 
void displayBoard(char[]); 
#include <stdio.h> 

int main() 
{ 
    char board[3][3]; 
    int i; 

    for (i=0;i<8;i++) 
    { 
     displayBoard(board); 
     selectLocation(board,i); 
    } 

return 0; 
} 

void displayBoard(char board[]) 
{ 
    printf(" 0 1 2"); 
    printf("\n --- --- --- "); 
    printf("\n0 | | | |"); 
    printf("\n --- --- --- "); 
    printf("\n1 | | | |"); 
    printf("\n --- --- --- "); 
    printf("\n2 | | | |"); 
    printf("\n --- --- --- "); 
} 

void selectLocation(char board[],int choice) 
{ 
    int x,y; 

    printf("Enter a location for X or O in x,x format"); 
    printf("\nex. '0,1' '1,2'"); 
    scanf("%d,%d",&x,&y); 

    if (choice%2==1) 
    { 
     board[x][y]='X'; 
    } 
    else 
    { 
     board[x][y]='O'; 
    } 
} 
+0

LearningC給你正確的答案,另一個問題是,你不檢查結果'scanf'(如果用戶輸入「abc,def」,你可能會遇到麻煩) –

回答

2
board[x][y]='X'; 

板是不是一個二維數組,在那裏你得到這個錯誤。

board[x] 

只是有效的。因爲你收到了一個char數組而不是2D數組。
因此改變函數的簽名都定義和申報接收二維數組,

void selectLocation(char board[][3],int choice) 
{ 
    int x,y; 
    ... 

void displayBoard(char board[][3]) 
{ 
+0

現在它顯示數組類型的元素類型不完整。 – alwaysLearning

+0

@BobbyII在哪個函數中? – LearningC

+0

selectLocation() – alwaysLearning

相關問題