0

我试图通过对将要更改的函数调用的各种访问来维护我的多维字符数组的内容。我怎样才能做到这一点。我需要将数组传递给函数吗?如果是这样,我该怎么做。但是我尝试遇到语法错误。这是我的函数声明

void board(int,int,int);

我需要添加对我的多维字符数组的引用吗?

char piece[3][3];

当我调用函数时,我如何将它传递给数组?

board(whichplayer,rownum,columnnum);

在函数本身中,我如何添加它并使用它并声明它?

void board(int movesquare, int row, int column)
 {
  //...
 }
4

1 回答 1

1

如果您的数组被声明为全局变量,那么在声明的同一文件中的每个函数中,您不必将数组作为函数参数传递。
例子:

char myArray[3][3];
void some_function(int var1,int var2);

int main(void)
{
  some_function(2,3);
.
.
.

}
void some_function(int var1, int var2)
{
extern int myArray[][3]; /* not necessary but just a reminder that 
                         you will use global variable in your function, 
                         size not necessary because this information in given
                          in  declaration on top */

myArray[var1][var2]=x; /* legal to modify global array */
.
.
.
}

如果您不希望 myArray 是全局的(出于多种原因,很多人会告诉您不这样做),您必须将该数组作为参数传递给您的函数。
示例 2:

void some_function(char passed_array[][3],int size,int var1, int var2)
    /* array will be treated as pointer to 1d 3-elements array of chars,
    so empty first size   is unnecessary in first argument,
    but you will probably want to know what's the 
    true size so you pass it as 2nd argument to function */

int main(void)
{
  char myArray[3][3];
  some_function(myArray,3,1,1);
  .
  .
  .
}
void some_function(char passed_array[][3],int size, int var1, int var2)
{
  passed_array[var1][var2]=x; /* passed_array is a pointer so changes made here 
  will be visible as changes in myArray in main */
.
.
.
}
于 2013-10-05T22:24:31.883 回答