# Finding the size of multi dimensional array?

Hi guys,

In most of my Arduino program I will always try to implement array. Array had help me to minimise my code substantially all this while. I notice that I have always been using array like the example below:

``````const uint8_t switchPin [] = {  3, 4, 5, 6, 7, 8, 9};
const uint8_t numOfSwitch = sizeof ( switchPin ) / sizeof ( uint8_t );
``````

anyway I always wanted to know do we get a multi dimensional array index?

such example

``````const uint8_t RGBCPin [][] =
{
( 2, 3, 4, 5),
( 5, 3, 4, 2),
( 2, 5, 4, 3),
( 2, 3, 5, 4),
};

const uint8_t numOfSwitch = sizeof ( RGBCPin ) / sizeof ( uint8_t );
``````

I know I will get back 16 cause the number of element in the array is 16 , But what I looking for is the value in X and Y in the following line.

``````const uint8_t RGBCPin [Y][X] =
{
( 2, 3, 4, 5),
( 5, 3, 4, 2),
( 2, 5, 4, 3),
( 2, 3, 5, 4),
};
``````

such that some coding magic allow me to get the size of the array ( the value of X and Y ) without directly specifying before hand making it much more flexible for future expansion.

for does that asking This is a very simple code for charlieplexing.

The idiom is:

``````sizeof (array) / sizeof (array)
``````

That gives you the outermost dimension, just index into this for the next level and use the same technique.

You'll have to use the correct syntax for array initializers too!!

MarkT:
The idiom is:

``````sizeof (array) / sizeof (array)
``````

That gives you the outermost dimension, just index into this for the next level and use the same technique.

Not sure what you meant by the "outermost" dimension. Anyway, I expanded the array and tried the following code:

``````const uint8_t array  =
{
( 2, 3, 4, 5),
( 5, 3, 4, 2),
( 2, 5, 4, 3),
( 2, 3, 5, 4),
( 0, 0, 0, 0),
};

void setup() {
// put your setup code here, to run once:
int size;

Serial.begin(115200);
size = sizeof (array) / sizeof (array);
Serial.print(" First level: ");
Serial.println(size);
size = sizeof (array) / sizeof (array);
Serial.print("Second level: ");
Serial.println(size);
}

void loop() {

}
``````

It appears that the standard macro gives the first rank of the array. Honestly, I never thought of the second macro to get the second rank, but it seems to work.

Not sure what you meant by the "outermost" dimension.

He meant the number of rows in the two dimensional array. Or how many sub-arrays has the main array.

``````const uint8_t array  =
{
( 2, 3, 4, 5),
( 5, 3, 4, 2),
( 2, 5, 4, 3),
( 2, 3, 5, 4),
( 0, 0, 0, 0),
};
``````

I don't think this is a valid array definition though

this would probably be better

``````const uint8_t array2  =
{
{ 2, 3, 4, 5},
{ 5, 3, 4, 2},
{ 2, 5, 4, 3},
{ 2, 3, 5, 4},
{ 0, 0, 0, 0},
};
``````

blimpyway:
He meant the number of rows in the two dimensional array. Or how many sub-arrays has the main array.

Actually, the first macro defines the number of columns, and the second the number of rows.

Edit: J-M-L. I think you're right. I think it is the second dimension that tells where to "fold" the array when accessing it, which would mean that blimpyway is correct. Interesting that the compiler doesn't seem to care.

I would also avoid using array as the name of your array - in some C++ variant it’s a reserved keyword (Microsoft never worried about the C++ standard )

Thanks guys,

MarkT:
The idiom is:

``````sizeof (array) / sizeof (array)
``````

That gives you the outermost dimension, just index into this for the next level and use the same technique.

You'll have to use the correct syntax for array initializers too!!

@MarkT, what do you mean by correct syntax for array initialiser?

@ econjack

Wow thanks, I would not of thought of that at all.

Ok guys, I got my question answered now.

For All the guys that answer my question will be rewarded with Karma. I know its free but Its the only way I could express my gratitude for now. This forum help me grow leaps and bound for years.. I'm try to contribute to this forum more often

You could use a macro:

``````#define ArrayCount(array) (sizeof array / sizeof array)
const SomeType TwoDArray;

ArrayCount(TwoDArray) should evaluate to 5
ArrayCount(TwoDArray) should evaluate to 4
``````
2 Likes