2

I want to pass a 2D pointer/array to a function in a readonly mode. I tried that for a 1D case, and it worked as I wanted, but for a 2D array it didn't!

The error message is

cannot convert argument 1 from 'int**' to 'const int **'

and my code is here

void test_constdpointer(int const** pda){
    cout << "function: \n";
    for (int i = 0; i < 3; i++) {
        cout << pda[i][0] << " " << pda[i][1] << " " << pda[i][2] << "\n";
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    int **da;
    da = new int*[3];
    for (int i = 0; i < 3; i++) {
        da[i] = new int[3];
    }

    for (int i = 0; i < 3; i++) {
        cout << da[i][0] << " " << da[i][1] << " " << da[i][2] << "\n";
    }
    test_constdpointer(da);
    return 0;
}
Vahid Mirjalili
  • 6,211
  • 15
  • 57
  • 80

1 Answers1

3

You need to take both pointer levels by const.

void test_constdpointer(int const* const* pda)
Captain Obvlious
  • 19,754
  • 5
  • 44
  • 74