I consider it a bit of a shortcoming of the Reference section on the website in that it doesn't expand upon the Array section on how to declare a multidimensional array. Worse yet, after spending nearly two hours going through forum posts, it isn't well explained there either. (There are a couple of hints, though)
Rather than leave it at that, I've decided to create this thread to make a definitive place to look for assistance on this topic.
For instance, you're trying to declare a multidimensional array of X integers in Y rows (or a 2 dimension array).
You'll find a lot of code when searching that suggests you to use a for loop to feed in the data like this:
int Matrix[Y][X];
for (int i =0; i < Y; i++) {
for (int j =0; j < X; j++) {
Matrix[i][j] = some data;
}
}
But, in some cases, you already know what that data is and you'll search the array section and wonder, does the Arduino not properly handle initializing multidimensional arrays? It does, even if it is a bit quirky.
Here's how you do it:
int Matrix[][X] = { {1,2,3,4,...,X},
{3,4,5,6,...,X},
{1,9,2,8,...,X},
{9,8,7,6,...,X} };
The things that are important to note are:
- The compiler does not seem to care about how many rows you have in the array, it cares about how many items (or columns) in each row
- Declaring the number of rows always causes an error (not altogether sure why)
- Not declaring how many items in each row will give you this cryptic error: "multidimensional array must have bounds for all dimensions except the first"
- Increasing the number of dimensions will make your memory usage grow exponentially!
- This is one of two ways that I have gotten the compiler to compile without giving errors, the second method uses #define for each row
HTH