Passing some part of a multiDimensional Array to a Function

Hi! How can I Passe some part of multi-Dimensional Array to a Function; for example only two dimension of three dimension of a an array with 3 dimension. This is because my function is defined for working with 2 dimension arrays.

Regards.
Last edited on
With built-in arrays, for any a[K][M][N], the expression a[x] is a 2D array (because 3D array is, quite literally, an array of 2D arrays)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template<std::size_t R, std::size_t C>
void print2D(int (&a)[R][C])
{
    for(auto& row: a) {
        for(int n: row)
            std::cout << n << ' ';
        std::cout << '\n';
    }
}

int main()
{
    int a[3][3][3] = {};
    print2D(a[1]);
}


but if you want some arbitrary 2D slice (e.g., a[x][1][[y]), you will need an array library that offers views such as boost.multi_array (or write your own array class, std::valarray gives an easy way to do it)
You mean that by having a[i], I will have the ith sub-arrays of a[3][3][3] which in this case a 2 dimensional sub-array of a[][][]?
Last edited on
For the array you used in the post (assume that it has type specifier int) you can define the function the following way

void f( int ( *a )[3], size_t n );

And you can call it as

f( a[0], 3 );
f( a[1], 3 );
f( a[2], 3 );
Topic archived. No new replies allowed.