Answer the question
In order to leave comments, you need to log in
How to put an n-dimensional array into a function argument?
----------------------------------------------
Answer the question
In order to leave comments, you need to log in
Well, again, experts advise adding asterisks and pointers to pointers ):
All examples below pass a three-dimensional array to the function and assign v the value of its element p[1][2][3].
If the function takes an array of fixed dimensions, then you can write it directly like this:
int f(int p[][20][30])
{
int i = 1, j = 2, k = 3;
int v = p[i][j][k];
}
...
int p[10][20][30];
f(p);
int f(int *p, int n2, int n3) // p[][n2][n3]
{
int i = 1, j= 2, k = 3;
int v = p[(((i * n2) + j) * n3) + k]; // v = p[i][j][k];
}
...
int p[10][20][30];
f(&p[0][0][0], 20, 30);
int f(int n2, int n3, int p[][n2][n3])
{
int i = 1, j = 2, k = 3;
int v = p[i][j][k];
}
...
int p[10][20][30];
f(20, 30, p);
but the compiler does not swallow.
Something like this:
int function(int [2][3][4][5]); // maybe not all dimensions need to be specified, I don't remember exactly
or
int function(int ******); // the number of stars determines the price of cognac the number of array dimensions
In general, more than two stars is undesirable, you can get confused.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question