如果您的数组被声明为全局变量,那么在声明的同一文件中的每个函数中,您不必将数组作为函数参数传递。
例子:
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 */
.
.
.
}