Pass array with different dimension into function
#include <stdio.h>
void print_onedim(int a[]);
void print_twodim(int a[][4]);
void print_threedim(int a[][3][4]);
main() {
int cnt=0;
int a[2][3][4];
int i;
int j;
int k;
for(i = 0;i < 2; i++){
for(j = 0;j < 3; j++){
for(k = 0;k < 4; k++) {
a[i][j][k] = cnt;
cnt++;
}
}
}
print_onedim(a[1][1]);
print_twodim(a[1]);
print_threedim(a);
}
void print_onedim(int a[]) {
int i;
for(i = 0; i < 4 ; i++)
printf("%d ", a[i]);
}
void print_twodim(int a[][4]) {
int j;
for(j = 0;j < 3; j++)
print_onedim(a[j]);
printf("\n");
}
void print_threedim(int a[][3][4]) {
int j;
printf("Each two dimension matrix\n");
for(j = 0; j < 2 ; j++)
print_twodim( a [ j ] );
}
Related examples in the same category