0

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!

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
  • 1
    C and C++ are different. So which one is it? Seems C++ related so why a C tag? – Support Ukraine Oct 15 '20 at 13:43
  • It was just suggested, and I think of C++ as an extension of C where there are some backwards compatibility (i.e. in C++ you can write C code, etc.). However, yes - one of my constraints is that I will have to compile using C++11. – sleep furiously Oct 15 '20 at 13:49
  • 3
    while C and C++ have a lot in common, there are important things that differ (you are looking at one). So don't see C++ as an extended C - it's not – Support Ukraine Oct 15 '20 at 13:52
  • 3
    Please note that variable length arrays are not part of the C++ standard, in C++ the size of the arrays must be known at compile time. In another words, you can't declare `int arr[size][size]` as a parameter where `size` is passed as a parameter as well. There are plenty of dupes on the subject: https://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function – Bob__ Oct 15 '20 at 13:55
  • Thanks @Bob__, that was my issue! I am thinking of `size` as a fixed / constant value, but there is no guarantee subsequent calls will have the same `size`, and that does seem like a huge issue for a static array! – sleep furiously Oct 15 '20 at 14:08

0 Answers0