一个调用多维数组的函数

时间:2014-12-10 16:52:29

标签: c

如何将多维数组传递给任何函数,如arr [n] [m] [p],如何将此数组传递给函数?

2 个答案:

答案 0 :(得分:0)

像普通数组一样。

从3D数组中获取值的示例:

int a=arr[*][*][*]
---------------------------------------------------------------------------------------
int <name of variable>=<array name><first cordinate><second cordinate><thrid cordinate>

 * = Whatever number you want

希望我能帮到你!

答案 1 :(得分:0)

传递数组及其尺寸(元素数量)。例如:

#include <stdio.h>

func(int m, int n, int p, int a[n][m][p])
{
    int j, k, l;
    for (j = 0; j < m; j++, printf("\n"))
    for (k = 0; k < n; k++, printf("\t"))
    for (l = 0; l < p; l++)
        printf("%3d", a[k][j][l]);    // print to check
}

main()
{
    int m = 2, n = 3, p = 4, arr[n][m][p], i = 0, j, k, l;
    for (k = 0; k < n; k++)
    for (j = 0; j < m; j++)
    for (l = 0; l < p; l++)
        arr[k][j][l] = i++; // initialize the array somehow
    func(m, n, p, arr);     // pass array along with dimensions
}