7

I have a 256x256 2-dimensional array of floats that I am trying to pass into a function and g++ is giving me the error message: Cannot convert 'int (*)[256]' to 'int**'. How can I resolve this?

void haar2D(int** imgArr);

int imageArray[256][256];
haar2D(imageArray);

I have tried changing the function parameter to types int[256][256] and int*[256] without successs.

Vlad from Moscow
  • 224,104
  • 15
  • 141
  • 268
Bobazonski
  • 1,405
  • 5
  • 23
  • 39
  • Have you tried changing the function parameter type to `std::vector> &imgArr` ? – Fantastic Mr Fox Dec 09 '15 at 20:32
  • I haven't worked with vectors before and wanted to use 2d arrays if possible. I also already wrote the function body using row-column (e.g. `imagearray[i][j]`) notation, can this be used with a vector? – Bobazonski Dec 09 '15 at 20:35
  • 1
    You can refer to [this](http://stackoverflow.com/questions/5329107/passing-a-pointer-representing-a-2d-array-to-a-function-in-c), it's C, you can use references instead, and possibly templates too, for the array sizes. Have better luck in searching next time. – LogicStuff Dec 09 '15 at 20:37
  • http://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function – VladimirS Dec 09 '15 at 20:51
  • Think about how a function like `void haar2D(int** imgArr);` could determine the dimensions of the array it gets passed and you'll begin to understand why the array dimensions are important. Because a 2-dimenstional array isn't physically the same as referencing data with a pointer to a pointer. In the latter case, additional data is needed to determine the size of the data referenced. – Andrew Henle Dec 09 '15 at 20:58
  • 1
    Go to the whiteboard and write 100 times "An array is not a pointer". – Pete Becker Dec 09 '15 at 21:01

2 Answers2

7

The function parameter must be declared as the compiler says.

So declare it either like

void haar2D( int imgArr[256][256] );

or

void haar2D( int imgArr[][256] );

or like

void haar2D( int ( *imgArr )[256] );

Take into account that parameters declared like arrays are adjusted to pointers to their elements.

Or your could declare the parameter as reference to array

void haar2D( int ( & imgArr )[256][256] );
Vlad from Moscow
  • 224,104
  • 15
  • 141
  • 268
2

If you don't want to change the function.

void haar2D(int** imgArr);

You can try to change the imageArray.

int **imageArray=new int*[256];
for (int i = 0; i < 256; ++i)
{
    imageArray[i] = new int[256];
}

Then

haar2D(imageArray);