我有这些数组:
unsigned long SMDATA_0_SIZE=60;
unsigned long SMDATA_0[SMDATA_SIZE] = {
<data>
};
unsigned long SMDATA_1_SIZE=60;
unsigned long SMDATA_1[SMDATA_1_SIZE] = {
<data>
};
unsigned long LGDATA_0_SIZE=60;
unsigned long LGDATA_0[SMDATA_SIZE] = {
<data>
};
unsigned long LGDATA_1_SIZE=60;
unsigned long LGDATA_1[SMDATA_1_SIZE] = {
<data>
};
我试图将其作为参数传递给一个函数。假设fmemopen
取决于定义:
#ifndef STRCONCAT
#define STRCONCAT(STR1,STR2) STR1 ## STR2
#endif
#ifdef SMALL_DATA
#define DATA_STR SMDATA
#endif
#ifdef LARGE_DATA
#define DATA_STR LGDATA
#endif
...
FILE* fd0 = fmemopen(STRCONCAT(DATA_STR,"_0"), strconcat(DATA_STR,"_0_SIZE"), "r");
FILE* fd1 = fmemopen(STRCONCAT(DATA_STR,"_1"), strconcat(DATA_STR,"_1_SIZE"), "r");
产量:
myapp.c:164:26: error: pasting formed '"DATA_STR""\"_0\""', an invalid preprocessing token
fd0 = fmemopen(STRCONCAT(DATA_STR,"_0"), STRCONCAT(DATA_STR,"_0_SIZE"), "r");
尝试:
FILE* fd0 = fmemopen(STRCONCAT(DATA_STR,_0), strconcat(DATA_STR,_0_SIZE), "r");
FILE* fd1 = fmemopen(STRCONCAT(DATA_STR,_1), strconcat(DATA_STR,_1_SIZE), "r");
也没有工作:
myapp.c:164:26: error: pasting formed '"DATA_STR""_0"', an invalid preprocessing token
fd0 = fmemopen(STRCONCAT(DATA_STR,_0), STRCONCAT(DATA_STR,_0_SIZE), "r");
我该如何改变它来形成文字变量呢?
1条答案
按热度按时间eivnm1vs1#
要将
_0
或_0_SIZE
粘贴到某个对象,请传递宏_0
或_0_SIZE
,而不是"_0"
或"_0_SIZE"
。##
操作符是粘贴操作符,而不是删除引号然后粘贴操作符。要粘贴
DATA_STR
扩展到的内容,请插入另一个宏,以便在粘贴之前执行扩展。更改:致:
要使用
STRCONCAT
宏,请使用STRCONCAT
,而不是strconcat
。