# How pass two dimensional array to library function

Hi,

I have the problem with passing two dimensional array to library function.

How to do it correctly?

Test.h

``````class Test
{
public:
Test();
void doTest(int** Input);
};
``````

Test.cpp

``````Test::Test()
{

}

void Test::doTest(int** Input)
{
int x = Input[0][0];
}
``````

Test.ino

``````#include "Test.h"

Test test();

void setup() {

int Input[11][4]= {
{ 1, 0, 0, 0 },
{ 1, 0, 0, 0 },
{ 1, 0, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 0, 1 },
{ 0, 0, 0, 1 }
};

test.doTest(Input);

}

void loop() {

}
``````

The function has to know the array dimensions, or it can't calculate indices. So you have to declare all but the first array indices in the function declaration.

`void Test::doTest(int Input [][4])`IIRC

I think a safe solution would be to pass the first and second dimensions like this:

``````void doTest(int** Input, int iDim1, int iDim2);

// inside the function valid indices range would be

int iLowerLeft = Input[0][0] ;
int iUpperRight = Input[iDim1-1][iDim2-1] ;

// example...
int AnArray[5][6] ;
doTest (anArray,5,6) ;
``````

Your function would know explicitly the limits to the 2D array.

As it stands it only knows there is a 2D array, but has no idea of the 2 sizes.

aarg:
The function has to know the array dimensions, or it can't calculate indices. So you have to declare all but the first array indices in the function declaration.

``````void Test::doTest(int Input [][4])
``````

IIRC

This works, but my Input can be different, with different number of columns and rows

I have tried this, but it didn't work

Test::Test(int y)
{
_y = y;
}

void Test::doTest(int Input[][_y])
{
int x = Input[0][0];
}

shh_x3m:
This works, but my Input can be different, with different number of columns and rows

I have tried this, but it didn't work

Test::Test(int y)
{
_y = y;
}

void Test::doTest(int Input[][y])
{
int x = Input[0][0];
}

I'm not surprised, as I said before, how could the compiler compute array indices? It can't do them dynamically. That's what you are asking it to do with [y].