We can pass reference of an array to a function like:
void f(int (&a)[5]);
int x[5];
f(x); //okay
int y[6];
f(y); //error - type of y is not `int (&)[5]`.
Or even better, we can write a function template:
template<size_t N>
void f(int (&a)[N]); //N is size of the array!
int x[5];
f(x); //okay - N becomes 5
int y[6];
f(y); //okay - N becomes 6
Now my question is, how to return reference of an array from a function?
I want to return array of folllowing types from a function:
int a[N];
int a[M][N];
int (*a)[N];
int (*a)[M][N];
where M
and N
is known at compile time!
What are general rules for passing and returning compile-time reference of an array to and from a function? How can we pass reference of an array of type int (*a)[M][N]
to a function?
EDIT:
Adam commented : int (*a)[N]
is not an array, it's a pointer to an array.
Yes. But one dimension is known at compile time! How can we pass this information which is known at compile time, to a function?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…