0

I am having problem in this situation

typedef struct {  
   ...
} str;

str *matrix[SIZE][SIZE][SIZE];

for (i = ...) {
    for (j = ...) {
        for (k = ...) {
            matrix[i][j][k] = (str *)malloc(NUM * sizeof(str));
        }
    }
}

and successfully get access to this 3D array by

( (matrix[i][j][k])[n] ) //  in n-th element.

My question is ,how can I pass this 3D array of pointers (to struct str) in a function in my program?

chqrlie
  • 131,814
  • 10
  • 121
  • 189
alphonse
  • 61
  • 10
  • Maybe an [answer of mine](http://stackoverflow.com/a/35615201/4774918) can help (the dup uses a fixed-dimension array). Note I try to explain a general rule for N-dimensional arrays. – too honest for this site Mar 08 '16 at 22:56

2 Answers2

0

Did you try the obvious syntaxes:

Accessing the nth structure:

matrix[i][j][k][n].member = value;

Receiving the 3D matrix:

void myfunc(str *a[SIZE][SIZE][SIZE]) {
    ...
    a[i][j][k][n].member = value;
    ...
}

Note that the first SIZE is ignored, arrays decay as pointers when passed to functions, the function argument could be defined as str *a[][SIZE][SIZE] or str *(*a)[SIZE][SIZE] with the exact same semantics.

Passing it:

myfunc(matrix);
chqrlie
  • 131,814
  • 10
  • 121
  • 189
0

My question is ,how can i pass this 3d array of pointers (to struct str) in a function in my program?

The argument type of the function needs to be

str *matrix[SIZE][SIZE][SIZE]

or

str *matrix[][SIZE][SIZE]

or

str * (*matrix)[SIZE][SIZE]

Example:

#define SIZE 10

typedef struct s str;

void foo(str* m[SIZE][SIZE][SIZE])
{
}

void bar(str* m[][SIZE][SIZE])
{
}

void baz(str* (*m)[SIZE][SIZE])
{
}

int main()
{
   str* m[SIZE][SIZE][SIZE];
   foo(m);
   bar(m);
   baz(m);
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270