# Multidimensional array with different sizes using Arduino

I would like to have a multidimensional array that allows for different sizes. Example:

``````int x[][][] = {{{1,2},{2,3}},{{1,2}},{{4,5},{2,7},{1,1}}};
```
```

The values will be known at compile time and will not change. I would like to be able to access the values like val = x;

What is the best way to go about this? I'm used to java/php where doing something like this is trivial. Thanks

I suppose you could do this "the old fashioned (uphill both ways) way":

``````#include <stdio.h>
int main(void){
int *x;
int y = {1,2,3,4,5,6,7,8,9,10,11,12};
x = &y;
x = &y;
x = &y;
x = &y;
x = &y;
x = &y;
// testing:
printf("x = %d\n", x);
printf("x = %d\n", x);
printf("x = %d\n", x);
printf("x = %d\n", x);
printf("x = %d\n", x);
printf("x = %d\n", x);
printf("x = %d\n", x);
printf("x = %d\n", x);
printf("x = %d\n", x);
printf("x = %d\n", x);
printf("x = %d\n", x);
printf("x = %d\n", x);
return 0;
}
```
```

Basically, the array `x` is a little bit too big (3x3) and it points to the "right place" in the array `y` that contains your data (I am using the digits 1…12 because it's easier to see it is doing the right thing). For a small example like this, you end up with an array of 9 pointers in `x` (72 bytes), plus the 12 integers in y (48 bytes).

If you filled an `int` array with zeros where you didn't need values (or `-1` if you wanted to indicate "invalid") you would end up with 18x4 = 72 bytes. So the above method is less efficient - because this array is not "very sparse". As you change the degree of raggedness, this gets better. If you really wanted to be efficient you would have an array of pointers-of-pointers, followed by n arrays of pointers - but this gets very messy very quickly.

Very often the right approach is a tradeoff between speed and memory size (which is always at a premium on the Arduino).

By the way - the above code does indeed produce the output

``````x = 1
x = 2
x = 3
x = 4
x = 5
x = 6
x = 7
x = 8
x = 9
x = 10
x = 11
x = 12
```
```

Of course it doesn't stop you from accessing an invalid array element - and doing so will generate a seg fault (since the unused elements in `x` are probably invalid pointers).