c - Passing multidimensional static array to a function -
i need little advice. 3 ways of passing static, multidimensional array (here have 3-dimensional, guess 4 dimensions analogical) function correct?
here's code:
#include <stdio.h> void fun1(int ***tab, int n, int m, int p) { int i,j,k; for(i=0; i<n; i++) { for(j=0; j<m; j++) { for(k=0; k<p; k++) { printf("tab[%d][%d][%d] = %d\n", i, j, k, tab[i][j][k]); } printf("\n"); } printf("\n"); } } void fun2(int tab[2][3][2]) { int i,j,k; for(i=0; i<2; i++) { for(j=0; j<3; j++) { for(k=0; k<2; k++) { printf("tab[%d][%d][%d] = %d\n", i, j, k, tab[i][j][k]); } printf("\n"); } printf("\n"); } } void fun3(int tab[][3][2]) { int i,j,k; for(i=0; i<2; i++) { for(j=0; j<3; j++) { for(k=0; k<2; k++) { printf("tab[%d][%d][%d] = %d\n", i, j, k, tab[i][j][k]); } printf("\n"); } printf("\n"); } } int main() { int tab[2][3][2] = { {{0, 1}, {2, 3}, {3, 4}}, {{5, 6}, {7, 8}, {9, 10}} }; fun1(tab,2,3,2); printf("--------------------------\n"); fun2(tab); printf("--------------------------\n"); fun3(tab); return 0; }
there seem problem fun1
can't understand mean: expected ‘int ***’ argument of type ‘int (*)[3][2]’|
. mean fun2
, fun3
valid in case?
one pointer pointer pointer int int ***
, , other pointer array of dimensions 3 , 2 int (*)[3][2]
.
those types not compatible , memory layout not same. array contiguous in memory while pointers pointers not.
if wish use first function in dynamic way dimensions known @ runtime declare additional parameters:
void fun1( int n, int m, int p , int a[n][m][p] )...
Comments
Post a Comment