pointer array to many 2d boolean arrays

Hello guys, I´m struggling to make a pointer to several 2d boolean arrays.

boolean array0[2][2] = {{0,0},{0,0}};
boolean array1[3][3] = {{0,0,0},{0,1,0},{0,0,0}};
boolean array2[2][2] = {{0,0},{0,0}};
boolean *arrays[3] = {&array0[0][0], &array1[0][0], &array2[0][0] };

...

boolean **arraySelect;
arraySelect = &arrays[1];
return arraySelect[1][1];   <----- this line does not return 1 , it returns a random number

Any advise ? Where I made a mistake?

Thank you!

  arraySelect = &arrays[1];

This is the address of the second pointer in the list of pointers so it is a pointer to a pointer to array1.

return arraySelect[1][1];

The first index picks up the address of array2 (the pointer after the second pointer in the list).
The second index gives your the second boolean in array2;

To get index [1][1] of array1 you will have to do the indexing math yourself:

return arrays[1][1*3+1];

This gives you the second boolean in the second row of booleans in array1. You need to know the number of elements in a row of your array to calculate the answer (note the '3' in the indexing math). Once you store the address of the array as a pointer it no longer carries dimension information. You could put in a parallel array:

const byte row_sizes[3] = {2, 3, 2};

return arrays[1][1*row_sizes[1]+1];

Hi johnwasser.

Clear explanation!! I understood the math.

The good news is that, in my original code , all arrays have the same number of row size.
So,maybe I can do the math without the "row_sizes" support.

I´ll try it at home and update here with results.

Thank you !

Only works if each array has the same element size, which is to say, if each array is an array of arrays of 3 booleans.

boolean array0[2][3] = {{0,0},{0,0}};
boolean array1[3][3] = {{0,0,0},{0,1,0},{0,0,0}};
boolean array2[2][3] = {{0,0},{0,0}};

boolean test1 = array0[0][0]; 
boolean *test2 = array0[0];
boolean (*test3)[3] = array0;

boolean (*arrays[])[3] = {array0, array1, array2 };

boolean getAnElement = arrays[0][0][0];

Maybe a tad beside the point but an array of boolean is rather memory in-efficient. Being that a boolean also takes up 1 byte, where 8 bits are available per byte and can easily be accessed individually.

Hi guys .

johnwasser: YES , it worked like a breeze. Thank you!
PaulMurrayCbr : YES , it worked like a breeze too. Thank you!
Deva_Rishi: YES , followed your advice and changed my arrays to byte. Thank you!

Made my day ! thank you for the knowledge!