Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing three dimensional arrays to a function in C

Tags:

arrays

c

I use to program with FORTRAN, but I decided to learn C and C++. I started with C language, and the one thing that I never used are pointers, because FORTRAN pass values by reference. I built the sample code below to understand how pointers work with multidimensional arrays:

#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 of the second function in the first one I get a compilation error:

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

Why *(arr + i) doesn't work in the first function?

like image 345
Pedro R. Avatar asked Nov 28 '16 13:11

Pedro R.


2 Answers

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.

like image 170
Paul Ogilvie Avatar answered Sep 30 '22 20:09

Paul Ogilvie


Just a complement to Paul Ogilvie's answer.

The correct usage of Variable Length Arrays would be:

void display3DArray3(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]);
            }
        }
    }
}
like image 20
Serge Ballesta Avatar answered Sep 30 '22 22:09

Serge Ballesta