当我像这样定义宏时:
[niko@dev1 test]$ cat m1.c
#define FL_UINT_FLINE_(N) typedef struct fl_uint_line_## N ##e { unsigned int indexes[N]; } fl_uint_line_## N ##e_t;
FL_UINT_FLINE_(4)
FL_UINT_FLINE_(8)
int main() {
fl_uint_line_4e_t fl4;
fl_uint_line_8e_t fl8;
}
[niko@dev1 test]$
它编译得很完美。但是我必须添加' e' ' ## N ##'之前和之后的字符(' for' for element)因为没有' e'我收到编译错误:
[niko@dev1 test]$ cat m2.c
#define FL_UINT_FLINE_(N) typedef struct fl_uint_line_## N ## { unsigned int indexes[N]; } fl_uint_line_## N ##_t;
FL_UINT_FLINE_(4)
FL_UINT_FLINE_(8)
int main() {
fl_uint_line_4_t fl4;
fl_uint_line_8_t fl8;
}
[niko@dev1 test]$ gcc -c m2.c
m2.c:1:42: error: pasting "fl_uint_line_4" and "{" does not give a valid preprocessing token
#define FL_UINT_FLINE_(N) typedef struct fl_uint_line_## N ## { unsigned int indexes[N]; } fl_uint_line_## N ##_t;
^
m2.c:2:1: note: in expansion of macro ‘FL_UINT_FLINE_’
FL_UINT_FLINE_(4)
^
m2.c:1:42: error: pasting "fl_uint_line_8" and "{" does not give a valid preprocessing token
#define FL_UINT_FLINE_(N) typedef struct fl_uint_line_## N ## { unsigned int indexes[N]; } fl_uint_line_## N ##_t;
^
m2.c:3:1: note: in expansion of macro ‘FL_UINT_FLINE_’
FL_UINT_FLINE_(8)
^
[niko@dev1 test]$
C中宏的正确语法是什么,使我的类型定义如下所示: (没有' e'):
typedef struct fl_uint_line_4 {
unsigned int indexes[4];
} fl_uint_line_4_t;
typedef struct fl_uint_line_8 {
unsigned int indexes[8];
} fl_uint_line_8_t;
答案 0 :(得分:5)
我在宏定义中添加了一个延续行,因此更容易阅读:
#define FL_UINT_FLINE_(N) typedef struct fl_uint_line_## N ## { \
unsigned int indexes[N]; } fl_uint_line_## N ##_t;
##
运算符连接两个标记以形成新标记。连接标识符(N
扩展为的内容)和{
会产生类似foo{
的内容,而不是有效令牌。删除第二个##
来修复此问题,不需要它:
#define FL_UINT_FLINE_(N) typedef struct fl_uint_line_## N { \
unsigned int indexes[N]; } fl_uint_line_## N ##_t;