Pedro R. - 1 year ago 111

C Question

I use to program with

`FORTRAN`

`C`

`C++`

`C`

`FORTRAN`

`#include <stdio.h>`

#include <stdlib.h>

#define DIM1 3

#define DIM2 2

#define DIM3 4

void display3DArray1(int, int , int n, int (*arr)[][n]);

void display3DArray2(int rows, int cols1, int cols2,int arr[][cols1][cols2]);

int main(void)

{

int matrix3D[DIM1][DIM2][DIM3] = {

{{1, 2, 3, 4}, {5, 6, 7, 8}},

{{9, 10, 11, 12}, {13, 14, 15, 16}},

{{17, 18, 19, 20}, {21, 22, 23, 24}}

};

int (*pmatrix3D)[DIM2][DIM3] = matrix3D;

display3DArray1(DIM1, DIM2, DIM3,pmatrix3D);

display3DArray2(DIM1, DIM2, DIM3,pmatrix3D);

return 0;

}

void display3DArray1(int rows, int cols1, int cols2,int (*arr)[][cols2]) {

printf("\n");

for(int i=0; i<rows; i++) {

for(int j=0; j<cols1; j++) {

for(int k=0; k<cols2; k++) {

printf("*arr : %d adress: %p\n",*(*((*arr+i*cols1))+j*cols2+k),*((*arr+i*cols1))+j*cols2+k);

}

}

}

}

void display3DArray2(int rows, int cols1, int cols2,int arr[][cols1][cols2]) {

printf("\n");

for(int i=0; i<rows; i++) {

for(int j=0; j<cols1; j++) {

for(int k=0; k<cols2; k++) {

printf("*arr : %d adress: %p\n", *(*(*(arr+i)+j) + k), *(*(arr+i)+j) + k) ;

}

}

}

}

The code works, but there is something that I wasn't able to understand. When I try to use the second

`printf`

"invalid use of array with unspecified bounds" -- under gcc.

Why

`*(arr + i)`

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

You can use the following two ways to pass/print the matrix:

```
void display3DArray1(int rows, int cols1, int cols2, int *A) {
int *a, i, j, k;
printf("\n");
for(i=0; i<rows; i++) {
for(j=0; j<cols1; j++) {
for(k=0; k<cols2; k++) {
a= A+(i*cols1*cols2)+(j*cols2)+k;
printf("%d, %p\n", *a, a);
}
}
}
}
void display3DArray2(int A[DIM1][DIM2][DIM3]) {
int i, j, k;
printf("\n");
for(i=0; i<DIM1; i++) {
for(j=0; j<DIM2; j++) {
for(k=0; k<DIM3; k++) {
printf("%d, %p\n", A[i][j][k], &A[i][j][k]);
}
}
}
}
```

The first method does not rely on the dimensions of the matrix; the second one does. As a result, the first one needs explicit address calculations (row *i*, col *j*, cell *k*).

Use calls respectively:

```
display3DArray1(DIM1, DIM2, DIM3, (int *)matrix3D);
display3DArray2(matrix3D);
```

Note the cast of the matrix to an int pointer.

In your code, you used parameter names to specify the dimensions of the matrix. In my C version, that is not legal; they must be constants.

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**