Answer the question
In order to leave comments, you need to log in
How to set the size of an array through function arguments?
Let's say there is a one-dimensional array arr (like m*n elements). It is passed to the function, as well as the length and width of the two-dimensional array created from it. In the end something like this:
int * foo(int * arr, int m, int n) {
int tempArray[m][n];
...
...
}
Answer the question
In order to leave comments, you need to log in
But it does not allow you to set the dimensions in this way ("m and n must have a constant value). How then can this be done?
In memory, the array will still be one-dimensional, even crack;) N-dimensionality is needed for ease of access.
template<size_t N, size_t M>
int* foo () {
int tempArray[ N ][ M ];
//...
}
// use case
foo<2, 3>();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question