C++ Pass different arrays of different sizes to same function

arrays, c++

Solution

A possibility would be a `template` function:

template <typename T, std::size_t Dim1, std::size_t Dim2>
void f(T(&)[Dim1][Dim2])
{
    std::cout << Dim1 << ", " << Dim2 << "\n";
}

See demo at http://ideone.com/b60h1e . Note this will instantiate three different instances of the function template (one instantiation for every different combination of dimensions).

Recommend changing to a `std::vector<std::vector<char>>` instead. This avoids the multiple instantiations and the function can query the `std::vector` for its size or iterate:

void f(std::vector<std::vector<char>>& a_maze)
{
    std::cout << a_maze.size() << ", " << a_maze[0].size() << "\n";
}

See demo at http://ideone.com/nHL8Hj .

Problem

I have 3 2D arrays that I wanted to pass to a function. Currently, I use if-else statement to decide which array to be sent then copy the selected array's content and pass the new array. new array: ``` char board[100][100]={} ``` these are the arrays(content not shown): ``` char mazeEasy[19][38], mazeMed[41][81], mazeHard[72][98]; ``` How can I pass either one of these three to one function without using `board[100][100]`?

Original source