#define(C预处理器)的数组格式 [英] Array format for #define (C preprocessor)

查看:140
本文介绍了#define(C预处理器)的数组格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能是一个幼稚的问题-我20年前曾经编程,从那以后就没有编码了。从那时起,我对 C预处理器的工作方式的记忆大大降低了。

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...

我正在写一个非常简单的 C 程序,我试图声明一些静态全局数组,但是 array 的大小是依赖的(以不平凡的方式)在 MODE 变量上。类似于下面的简化示例。

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.

两个要点:我知道我可以根据任何<$所需的最大大小来调整数组的大小。 c $ c> MODE ,但我不想这样做,因为(与下面的简化示例不同)有时这些尺寸中的少数尺寸会非常大,而其他尺寸很小。

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:

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

您可以通过样板代码获得一堆预处理器库(增强PP,P99 ),也可以自己滚动。主要问题是您需要根据要处理的最大数量的参数定义ARG宏。

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.

这篇关于#define(C预处理器)的数组格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆