[Solved] Passing to a same function matrices with different sizes of both dimensions


You should use a typedef so that you don’t have to use any awful syntax:

using matrix_t = int[3][3];

And you should pass your args by reference whenever possible:

void handle_matrix(const matrix_t &mat){
    // do something with 'mat'
}

If you want to use the original syntax without a typedef:

void handle_matrix(const int (&mat)[3][3]){
    // ...
}

and if you want to use the original syntax and pass by pointer:

void handle_matrix(const int (*mat)[3]){
    // ...
}

But then you lose type safety, so I’d recommend against this and just go with the nicest option: typedef and pass by reference.


EDIT

You said in a comment on @Kerrek SB’s answer that your matrices will be different sizes.

So here is how to handle that and still keep the nice method:

template<size_t Columns, size_t Rows>
using matrix_t = int[Columns][Rows];

template<size_t Columns, size_t Rows>
void handle_matrix(const matrix_t<Columns, Rows> &mat){
    // ...
}

Take into account that I’m presuming you can use C++14 in my answer, if you leave a comment I can modify it for any other version.

8

solved Passing to a same function matrices with different sizes of both dimensions