2

I have problems to understand following multi-dimensional array declaration in C++.

static const byte LAYER_SIZE = 3;
static const byte ROWS = 5;
static const byte COLS = 10;

static const byte (*colorMapping[LAYER_SIZE]) [ROWS][COLS];

What does this declaration do?

Especially the part

(*colorMapping[LAYER_SIZE])

is confusing for me - what is the meaning of this and what does it in combination with [ROWS][COLS]?

asterix
  • 23
  • 2
  • 1
    Colour mapping seems to be an array of pointers of size LAYER_SIZE and each of these pointers point to a 2D Array (Matrix) of size ROWS COLS, where the values are of size byte. – Avin Kavish Jun 07 '19 at 06:13
  • related: https://stackoverflow.com/questions/3707096/spiral-rule-and-declaration-follows-usage-for-parsing-c-and-c-declarations – kmdreko Jun 07 '19 at 06:15
  • 1
    Replacing the constants, and using a standard type instead of `byte`, [this](https://cdecl.org/?q=const+char+(*colorMapping[3])+[5][10]%3B) says "declare colorMapping as array 3 of pointer to array 5 of array 10 of const char" – Some programmer dude Jun 07 '19 at 06:15

1 Answers1

7

Perhaps the best way to understand is to compare examples of declarations with and without brackets. Just like in an expression brackets are used to affect the precedence of various parts of the declaration.

For instance, starting simple,

int* array[10];

is an array of ten pointers. But what if you want a pointer to an array instead of an array of pointers? That would be

int (*ptr)[10];

The brackets mean that ptr is firstly a pointer, and it's only when that pointer is dereferenced that you get to the array.

So this example

int* (array[10]);

is the same as the first example. The superfluous brackets make clear that array is firstly an array and that only when you get to the elements of the array that you have a pointer. What the above examples are saying is that in a declaration (just like in an expression) [] has higher precedence than * and if you want it the other way round you have to use brackets.

So going to the original example

static const byte (*colorMapping[LAYER_SIZE]) [ROWS][COLS];

Looking inside the brackets first we have an array of pointers (just like my first example above). Then looking outside the brackets we see that each pointer points to a two dimensional array.

If we missed out the brackets

static const byte *colorMapping[LAYER_SIZE][ROWS][COLS];

then we'd have a three dimensional array of pointers.

Hopefully that's clear.

john
  • 85,011
  • 4
  • 57
  • 81
  • Thank your for your comprehensive answer. So this means: There is a constant array defined having LAYER_SIZE which contains pointers to 2-dimensional arrays of dimension [ROW][COL] where each element (of the matrix) holds a byte? – asterix Jun 07 '19 at 07:30
  • @asterix That's right. What are constant are the bytes. The pointers are not constant (you would need `static const byte (*const colorMapping[LAYER_SIZE])[ROWS][COLS];` for that). – john Jun 07 '19 at 07:55
  • Thank you, this clarifies the matter for me! C/C++ syntax is really hard to understand sometimes! – asterix Jun 07 '19 at 08:06