How does one pass a pointer to a multi dimensional array to a function. Oddly, I have nearly zero experience with these multi dimensional array things..
This is how I -thought- I could do it..
My block of data..
byte grid[GRID_X/8][GRID_Y];
byte* gridPtr = &grid[0][0];
setGrid(gridPtr,13,12,true); // Example of a call to set grid()..
And my actual function..
void setGrid(byte* grid,int x,int y,bool value) {
int xIndex;
byte xBit;
if (x<0 || x>GRID_X) return;
if (y<0 || y>GRID_Y) return;
xIndex = x>>3;
xBit = x - (xIndex<<3);
if (value) {
bitSet(grid[xIndex][y],xBit); // And here the compiler stops.
} else {
bitClear(grid[xIndex][y],xBit);
}
}
It's actually a reference. From our perspective there is not much difference. As a parameter to a function the only difference is that my snippet carries more type information.
Yup I did. I went back with what you told me and tried a few things. grid[][GRIX_Y] was the ticket. I'm guess ing it passed just the pointer on the stack but the compiler had enough info to put together the matrix at compile time. (Because it worked )
That doesn't work because the compiler has no way of knowing the number of columns in 'grid'. It needs that information to index into the array: xIndex * ncols + y
That works because the compiler now knows the number of columns. Of course, now the function only works for arrays with that many columns.
A more general technique would be to pass the array size in the function call:
But, all of the above are old-school C language techniques. The proper C++ way would be to use a template function and let the compiler figure it all out. But, this obviously only works with arrays whose size is known at compile time.