我想要一个执行以下操作的函数:
int doFunction(int* nIndex)
{
//nIndex is a 2-D array of any size.
// I would like to fill this array inside this function.
//Eg: in nIndex[2][3], i would like to put 5.
}
int result;
int myIndex[5][6];
result = doFunction(myIndex);
有人可以帮我解决两件事吗: 1. 对于需要任意大小的二维数组的函数定义,上述语法是否正确? 2.当我将 myIndex 传递给函数时,语法是否正确? 3. 如何在函数中填充数组,或者如何访问其字段?
谢谢。
最佳答案
声明和定义:
// either void fill_arr(int arr[][5]) or:
void fill_arr(int arr[3][5])
{
int i j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 5; j++) {
arr[i][j] = 42;
}
}
}
调用:
int arr[3][5];
fill_arr(arr);
关于C:在函数中通过引用传递二维数组以及如何在函数内部使用它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14957148/