C预处理器 - 爆炸数组及其长度

时间:2013-12-01 14:44:38

标签: c arrays c-preprocessor static-array

我有一个问题。在我自己的网络服务器中,我在文件范围内有以下代码:

typedef struct {
    char *slice;
    int length;
} values_index;

const char *aHeaderKeys[] = {
    /* You can add here whatever you want! */
    "Accept",
    "Accept-Charset",
    "Accept-Encoding",
    "Accept-Language",
    "Authorization",
    "Expect",
    "From",
    "Host",
    "If-Match",
    "If-Modified-Since",
    "If-None-Match",
    "If-Range",
    "If-Unmodified-Since",
    "Max-Forwards",
    "Proxy-Authorization",
    "Range",
    "Referer",
    "TE",
    "User-Agent",
    "Connection"
};

int nHeadersSizes[sizeof(aHeaderKeys) / sizeof(*aHeaderKeys)];
values_index aHeaderValuesIndexes[sizeof(aHeaderKeys) / sizeof(*aHeaderKeys)];
const int nHeadersLen = sizeof(aHeaderKeys) / sizeof(*aHeaderKeys);

但是,由于aHeaderKeys是一个常量字符串常量长度的数组,因此在程序执行期间计算数组的长度是愚蠢的,最好手动编写它:

typedef struct {
    char *slice;
    int length;
} values_index;

const char *aHeaderKeys[20] = {
    /* You can add here whatever you want! */
    "Accept",
    "Accept-Charset",
    "Accept-Encoding",
    "Accept-Language",
    "Authorization",
    "Expect",
    "From",
    "Host",
    "If-Match",
    "If-Modified-Since",
    "If-None-Match",
    "If-Range",
    "If-Unmodified-Since",
    "Max-Forwards",
    "Proxy-Authorization",
    "Range",
    "Referer",
    "TE",
    "User-Agent",
    "Connection"
};

int nHeadersSizes[20];
values_index aHeaderValuesIndexes[20];
const int nHeadersLen = 20;

但是我的数组元素的数量可以在开发过程中改变 ,所以每次我想添加另一个元素时我都需要手动更改长度。 现在我的问题是:是否有可能编写一个预处理器宏,就像我在JavaScript中编写的那样(作为伪代码)?

var aHeaders = [
    "Accept",
    "Accept-Charset",
    "Accept-Encoding",
    "Accept-Language",
    "Authorization",
    "Expect",
    "From",
    "Host",
    "If-Match",
    "If-Modified-Since",
    "If-None-Match",
    "If-Range",
    "If-Unmodified-Since",
    "Max-Forwards",
    "Proxy-Authorization",
    "Range",
    "Referer",
    "TE",
    "User-Agent",
    "Connection"
];

var sExplodedText = "const char *aHeaderKeys[" + aHeaders.length + "] = {\n\t\/* You can add here whatever you want! *\/\n\t\"" + aHeaders.join("\",\n\t\"") + "\"\n};\n\nint nHeadersSizes[" + aHeaders.length + "];\nvalues_index aHeaderValuesIndexes[" + aHeaders.length + "];\nconst int nHeadersLen = " + aHeaders.length + ";";

alert(sExplodedText);

2 个答案:

答案 0 :(得分:2)

编译器无需指定数组的大小。 你可以做到

const char *aHeaderKeys[] = {
...
};


#define NR_HEADERS (sizeof aHeaderKeys/sizeof aHeaderKeys[0])

然后在任何需要的地方使用NR_HEADERS。这里的表达式将由编译器在编译时计算,没有进行运行时计算。

答案 1 :(得分:0)

所以,感谢@nos和@JonasWielicki,这是解决方案。

确定以下行中的NR_HEADERS

#define NR_HEADERS (sizeof aHeaderKeys / sizeof *aHeaderKeys)

将始终在编译时用20 替换,并且在以下代码中将进行无运行时计算

const char *aHeaderKeys[] = {
    /* You can add here whatever you want! */
    "Accept",
    "Accept-Charset",
    "Accept-Encoding",
    "Accept-Language",
    "Authorization",
    "Expect",
    "From",
    "Host",
    "If-Match",
    "If-Modified-Since",
    "If-None-Match",
    "If-Range",
    "If-Unmodified-Since",
    "Max-Forwards",
    "Proxy-Authorization",
    "Range",
    "Referer",
    "TE",
    "User-Agent",
    "Connection"
};

#define NR_HEADERS (sizeof aHeaderKeys/sizeof sizeof *aHeaderKeys)

values_index aHeaderValuesIndexes[NR_HEADERS];
int nHeadersSizes[NR_HEADERS];

所以,问题解决了!

谢谢!