Getting head around using Pointers and References

This is what I want to do:

create an array that stores 4 other predefined arrays. 2D array will not cut it.

My idea is to create a Struct that has array inside called „PointersArray[4]“.
In „PointersArray[4]“ I want to store a pointer to other already defined „TyleArray1[16]"-„TyleArray4[16]" arrays.

I want to create many objects with different array configuration inside „PointersArray[4]“.

I know code below is wrong in many levels, but this is the only way to get my goal across, I hope I was clear enough.

byte *PointersArray[4];

PointersArray[0] = &TyleArray1[16]; (stores pointer of TyleArray1[16])
PointersArray[1] = &TyleArray2[16]; (stores pointer of TyleArray2[16])
PointersArray[2] = &TyleArray3[16]; (stores pointer of TyleArray3[16])
PointersArray[3] = &TyleArray4[16]; (stores pointer of TyleArray4[16])


void loop()
{

  for (byte i = 0; i <  4; i++ ) // loops all 4 arrays inside PointersArray[]
  {
    for (byte b = 0; b <  16; b++ ) // loops all 16 members in TyleArray(1-4)[]
    {
      Serial.println(  *PointersArray[i] + b ); // here my goal is to get adress of array TyleArray1[] to TyleArray2[] and add "b" to that adress and retreave value that this new adress points to
    }
  }

}

Thanks in advance.

Firstly, "create an array that stores 4 other predefined arrays." sounds like a 2D array. But if you say that "2D array will not cut it" then so be it.

Judging by the code you already wrote, you need arrays of byte elements. Then

byte TyleArray1[16], TyleArray2[16], TyleArray3[16], TyleArray4[16];
byte *const PointersArray[] = { TyleArray1, TyleArray2, TyleArray3, TyleArray4 };

And we are done.

You can access the elements using the "regular" syntax, e.g. PointersArray[ii][jj]. Note that if you declare this all locally, initially the elements will contain garbage values.

You can do

byte TyleArray1[16] = { 0 }, TyleArray2[16] = { 0 }, TyleArray3[16] = { 0 }, TyleArray4[16] = { 0 };
byte *const PointersArray[] = { TyleArray1, TyleArray2, TyleArray3, TyleArray4 };

to get zeros as initial values. In C++ you can also use = {} instead of = { 0 }.


The above will work as C++ or C code. However, in C you can use an even more compact syntax to the same effect

byte *const PointersArray[] = 
{ 
  (byte [16]) { 0 },
  (byte [16]) { 0 },
  (byte [16]) { 0 },
  (byte [16]) { 0 }
};

This relies on C99 feature called compound literals.


But again, this technique is what is often referred to as "jagged array". In jagged array we can use row arrays of different length (to save memory when representing sparse matrices), we can leave some rows unassigned or we can even make some row pointers to refer to the same row array. In your case all row arrays are present and have the same length - 16. So, I don't understand why you are avoiding a regular 2D array in this case. Is this some external requirement?

P.S. The title of your question mentions "references". The above code does not use references, only pointers. We can do it with references, if you want. But there's really no reason...

Thank You for Your reply,

yes, I need this to wok with references cos I will import .h file with many arrays with 16 elements each.
And as I mentioned I will need to make many "PointersArray" that will have different 4 arrays inside.
Making it 2D array doesn't seems to be convenient in my situation.

Trying Your suggestion...