Array format for #define (C preprocessor)

2019-02-09 00:13发布

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];

3条回答
女痞
2楼-- · 2019-02-09 00:18

Probably the best you can do is something like this:

#define SIZE_LIST_1_0 2
#define SIZE_LIST_1_1 7
#define SIZE_LIST_1_2 23
#define SIZE_LIST_1_3 33
#define SIZE_LIST_1_4 12

#define SIZE_LIST_2_0 11
#define SIZE_LIST_2_1 65
#define SIZE_LIST_2_2 222
#define SIZE_LIST_2_3 112
#define SIZE_LIST_2_4 444

#define MODE 4

#define S1 SIZE_LIST_1_##MODE
#define S2 SIZE_LIST_2_##MODE

int a[S1], b[S2];
查看更多
看我几分像从前
3楼-- · 2019-02-09 00:39

You need to define a bunch of helper macros first before you can do this in a simple way:

#define CONCAT(A,B)         A ## B
#define EXPAND_CONCAT(A,B)  CONCAT(A, B)

#define ARGN(N, LIST)       EXPAND_CONCAT(ARG_, N) LIST
#define ARG_0(A0, ...)      A0
#define ARG_1(A0, A1, ...)  A1
#define ARG_2(A0, A1, A2, ...)      A2
#define ARG_3(A0, A1, A2, A3, ...)  A3
#define ARG_4(A0, A1, A2, A3, A4, ...)      A4
#define ARG_5(A0, A1, A2, A3, A4, A5, ...)  A5
#define ARG_6(A0, A1, A2, A3, A4, A5, A6, ...)      A6
#define ARG_7(A0, A1, A2, A3, A4, A5, A6, A7, ...)  A7
#define ARG_8(A0, A1, A2, A3, A4, A5, A6, A7, A8, ...)      A8
#define ARG_9(A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, ...)  A9
#define ARG_10(A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, ...)    A10

/* above should be in a pp_helper.h header file or some such */

#define SIZE_LIST_1 ( 2,  7,  23,  33,  12,   76)
#define SIZE_LIST_2 (11, 65, 222, 112, 444, 1000)

#define S1 ARGN(MODE, SIZE_LIST_1)
#define S2 ARGN(MODE, SIZE_LIST_2)

#define MODE 4

int a[S1], b[S2];

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.

查看更多
放荡不羁爱自由
4楼-- · 2019-02-09 00:43

I'm afraid there is no such possibility.

I suggest the following approach instead:

#define MODE 0

#define DECLARE_ARRAYS_WITH_SIZES(S1, S2, S3) \
    int arr1[S1]; \
    int arr2[S2]; \
    int arr3[S3];

#if MODE == 0
DECLARE_ARRAYS_WITH_SIZES(3, 6, 7)
#elif MODE == 1
DECLARE_ARRAYS_WITH_SIZES(8, 2, 1)
#endif
查看更多
登录 后发表回答