C:在函数中通过引用传递二维数组以及如何在函数内部使用它们

标签 c arrays

我想要一个执行以下操作的函数:

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/

相关文章:

c - fscanf 不读取任何内容

C 正则表达式如何匹配任何以“或任何空字符串结尾的字符串?

java - 如何在 Android 中获取 volley JSONArray

c++ - gluLookAt 似乎产生了错误的观点,OpenGL

c - 套接字错误是否意味着套接字已关闭

Javascript - 归约和数组

Php - 创建和显示多维数组

javascript - 根据 json 数组值选择 html 复选框

arrays - "list of variable length strings"的 native Fortran 类型签名?

c - Linux Kernel Module程序从IP获取域名