gpt4 book ai didi

c - 既不是参数也不是指针

转载 作者:太空宇宙 更新时间:2023-11-04 08:43:12 26 4
gpt4 key购买 nike

数组的新手,我不断收到错误消息,即在 selectLocation 函数中下标值既不是指针也不是 vector 。这只是井字游戏程序的开始。我做错了什么?

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

最佳答案

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

board 不是 2D 数组,你会得到那个错误。

board[x]

仅有效。因为您收到的是 char 数组而不是 2D 数组。
所以在定义和声明中更改函数的签名以接收二维数组,

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

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

关于c - 既不是参数也不是指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22728249/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com