Probably a naïve question – I used to program 20 years ago and haven’t coded much since. My memory of how the C preprocessor works has atrophied significantly since then…
I am writing a very simple C program and I am trying to declare a few static global arrays, but the size of the arrays would be dependent (on a non-trivial way) on a MODE variable. Something like the simplified example below.
Two quick points: I know I could just size the arrays according to the largest size needed by any MODE, but I don’t want to that because (unlike in the simplified example below) sometimes a handful of these dimensions are going to be extremely large while others are tiny.
Also, I want to use statically defined global arrays – rather than dynamically allocate them at runtime. I want the compiler to have the sizes at compile time.
//** Simplified example of what I'd like to do **//
#define SIZE_LIST_1[5] = {2, 7, 23, 33, 12, 76} // I don't think this is valid syntax
#define SIZE_LIST_2[5] = {11, 65, 222, 112, 444}
#define MODE 4
#define S1 SIZE_LIST_1[MODE]
#define S2 SIZE_LIST_2[MODE]
int a[S1], b[S2];
You need to define a bunch of helper macros first before you can do this in a simple way:
There are a bunch of preprocessor ‘libraries’ you can get with the boilerplate code (boost PP, P99), or you can just roll your own. The main problem being that you need to define ARG macros based on the largest number of arguments you’ll ever want to handle.