I have a macro to repeat macros that I use to fill arrays with default values in compile time:
const int array [512] =
{
MACRO_REPEAT(512, -2) // this repeats -2, 512 times
[4] = 10,
[5] = 2,
...
}
The macro repeat will expand to MACRO_REPEAT_512, but now I wanted to use other macros as the array size, like:
#define ARRAY_LENGTH 512
const int array [ARRAY_LENGTH ] =
{
MACRO_REPEAT(ARRAY_LENGTH , -2) // this repeats -2, 512 times
[4] = 10,
[5] = 2,
...
}
But this expands to MACRO_REPEAT_ARRAY_LENGTH, doesn't expand ARRAY_LENGTH
value before concatenating it. Other example would be for multi-dimensional arrays, which involves more levels of expansion:
#define X 512
#define Y 512
const int array [X][Y] =
{
MACRO_REPEAT(X*Y , -2) // this repeats -2, 512 times
[4] = 10,
[5] = 2,
...
}
This will expand to MARO_REPEAT_X*Y. So, is there a way to expand those values to the final numerical value before concatenating it to other macros?
memset()
? Yes, those work at run time, but achieve the desired effect relatively easily. – Peter Jul 15 '16 at 13:01int array[ARRAY_LENGTH] = { [0 ... 3] = -2, [4] = 10, [5] = 2, [6 ... ARRAY_LENGTH - 1] = -2, };
– Ian Abbott Jul 15 '16 at 13:09