I have a trivial test function that takes a row and column index for a 2D array, and checks to see if the value of the 2D at that location is 2. If not, it checks the next row index until it has checked every element along that row for the value 2:
1 bool rowContainsTwo(const int rowIndex, const int columnIndex, const int size, const int grid[size][size]) {
2 if (grid[rowIndex][columnIndex] == 2)
3 return true;
4 else
5 return (rowIndex + 1 < size) ? rowContainsTwo(rowIndex + 1, columnIndex, size, grid) : false;
6 }
When I compile this test function, I get the following compile error:
$ g++ -std=c++11 test.cpp -o test
test.cpp:5:84: error: cannot initialize a parameter of type 'const int (*)[*]' with an lvalue of type 'const int (*)[size]'
return (rowIndex + 1 < size) ? rowContainsTwo(rowIndex + 1, columnIndex, size, grid) : false;
^~~~
test.cpp:1:90: note: passing argument to parameter 'grid' here
bool rowContainsTwo(const int rowIndex, const int columnIndex, const int size, const int grid[size][size]) {
^
1 error generated.
I have seen all sorts of articles about passing 2D arrays as parameters of functions, but I have yet to see anything that tells me about this error I am getting, and how to pass the array to the function recursively.
I was hoping for a straightforward answer to how to pass a static array recursively as a parameter, without having to make it a dynamic array.
Note, I don't need to modify the array elements, I just need to look at the elements.
Also note that I am aware using a vector would be better, and that I could use for loops and all sorts of iterative methods - the constraints are set that I want to do this with a static 2D array, and I want to pass the array recursively as a parameter.
Thanks for reading!