0

I would like to define a multi-dimensional array via a macro with the Arduino language. I am trying to save sketch/RAM space.

I would like to do something like so:

#define MYARR {{8, 0}, {8, 1}, {8, 2}, {8, 3}, {8, 4}, {8, 5}, {8, 6}}

function_that_uses_array(MYARR);

but I just can't seem to get it to work, and get a multitude of errors. I have tried several different ways of defining the array in the macro, but I am admittedly not even sure this is possible to do.

Is this possible, and if so, how?

1 Answers1

0

Not an answer you may expect but instead of using multi-dimensional array maybe you could use a one-dimensional array mapping multiple dimensions and use setter/getter functions to work with values as explained in this thread ?

Helper functions are not necessary if you define dimension sizes as global constants to calculate index value for given dimensions' indexes - you can work with the 1D array directly then.

EDIT(5/4/15):

Other thing is I doubt array can be initialized using a pre-processor macro as:

All arrays consist of contiguous memory locations

(source)

For this reason array must be an initialized variable. It can be initialized in different ways - one using data defined in a macro is described in a link in my comment to this answer however a multi-dimensional array can be also initialized directly:

 #define MAX_ROWS 3
 #define MAX_COLS 3
 const int a[MAX_ROWS][MAX_COLS] = { {1,2,3},
                                     {4,5,6},
                                     {7,8,9}  };
Community
  • 1
  • 1
Mr. Girgitt
  • 2,853
  • 1
  • 19
  • 22
  • Interesting, though I suppose that still leaves me in a situation where I still can't figure out how to define an array via a macro in the arduino language. Could you be of help with that? –  Apr 04 '15 at 18:35
  • Have a look at the second answer here: http://stackoverflow.com/questions/27784649/initialize-a-2d-array-of-unknown-size-using-macros-in-c You still have to initialize a variable though (int v[][MAXCOLUMNS] = VALUE; in the linked the answer). Code compiles but I have no chance at the moment to check it with a real AVR. – Mr. Girgitt Apr 04 '15 at 23:49