数组的新手,我不断收到selectLocation函数中的下标值既不是指针也不是向量的错误。这只是一个tic tac toe程序的开始。我做错了什么?
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 :(得分:2)
board[x][y]='X';
board不是2D数组,因为你得到了那个错误。
board[x]
仅有效。因为你收到了char数组而不是2D数组 因此,在定义和声明中更改函数的签名以接收2D数组,
void selectLocation(char board[][3],int choice)
{
int x,y;
...
和
void displayBoard(char board[][3])
{