Multidimensional array with different sizes using Arduino

advertisements

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[2][0][1];

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[3][3];
    int y[12] = {1,2,3,4,5,6,7,8,9,10,11,12};
    x[0][0] = &y[0];
    x[0][1] = &y[2];
    x[1][0] = &y[4];
    x[2][0] = &y[6];
    x[2][1] = &y[8];
    x[2][2] = &y[10];
    // testing:
    printf("x[0][0][0] = %d\n", x[0][0][0]);
    printf("x[0][0][1] = %d\n", x[0][0][1]);
    printf("x[0][1][0] = %d\n", x[0][1][0]);
    printf("x[0][1][1] = %d\n", x[0][1][1]);
    printf("x[1][0][0] = %d\n", x[1][0][0]);
    printf("x[1][0][1] = %d\n", x[1][0][1]);
    printf("x[2][0][0] = %d\n", x[2][0][0]);
    printf("x[2][0][1] = %d\n", x[2][0][1]);
    printf("x[2][1][0] = %d\n", x[2][1][0]);
    printf("x[2][1][1] = %d\n", x[2][1][1]);
    printf("x[2][2][1] = %d\n", x[2][2][0]);
    printf("x[2][2][1] = %d\n", x[2][2][1]);
  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[0][0][0] = 1
x[0][0][1] = 2
x[0][1][0] = 3
x[0][1][1] = 4
x[1][0][0] = 5
x[1][0][1] = 6
x[2][0][0] = 7
x[2][0][1] = 8
x[2][1][0] = 9
x[2][1][1] = 10
x[2][2][1] = 11
x[2][2][1] = 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).