[Answered] Multi-dimensional arrays ordering in memory

Sorry if this is a newbie Question. I have tried to google for an answer, but have not seen one yet. Not even with the built-in google on this forum.

In one of my projects I am initialising a 2 dimensional array. If it is defined

const byte multiArray [y], how are the individual values then placed in mem/program space?

[x0y0][x0y1][x0y2][x0y3][x1y0][x1y1][x1y2][x1y3][x2y0], etc or
[x0y0][x1y0][x2y0][x3y0][x0y1][x1y1][x2y1][x3y1][x0y2], etc or
is this undefined (that is it might be either of the above, but which might change over time) ?

I understand, that if I access the array with multiArray [y], the above wont matter, as the compiler will handle it the right way. But if I could also access the individual item in the array with a one-dimensional reference like multiArray [x*xrows+y] it would matter.

Secondly is it actually possible to access multiArray with just a one dimensional reference?

Thanks in advance :slight_smile:

You need to use code tags so that “[ x ]” isn’t folded to “

Google “C array order”?

Use [code] [/code] tags

You can find out by yourself:

int array[2][3] =
{
 { 11, 22, 33 },
 { 44, 55, 66 }
};

int *ptr = array[0]; // pointer to the first element of the array

for ( int i = 0; i < 2*3; i++ )
 // print the value pointed to, and increase pointer
 Serial.println( *ptr++ );

This will print

11
22
33
44
55
66

Similarly, you can access an element of the array by using the same pointer, to which you add an offset

JanHolbo:
Sorry if this is a newbie Question. I have tried to google for an answer, but have not seen one yet. Not even with the built-in google on this forum.

Google is not an oracle. It’s just a search engine. C language is very well documented on the net. The idea is to search for documentation, not answers to narrow questions.