macro for dynamic types in C -
when define macro this:
[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]$
it compiles perfectly. had add 'e' character ('e' element) before , after '## n ##' because without 'e' compile error:
[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" , "{" not give 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" , "{" not give 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]$
what correct syntax macros in c, make type definitions this: (without '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;
i added continuation line macro definition it's easier read:
#define fl_uint_fline_(n) typedef struct fl_uint_line_## n ## { \ unsigned int indexes[n]; } fl_uint_line_## n ##_t;
the ##
operator concatenates 2 tokens form new token. concatenating identifier (what n
expands to) , {
yields foo{
not valid token. drop second ##
fix this, it's not needed:
#define fl_uint_fline_(n) typedef struct fl_uint_line_## n { \ unsigned int indexes[n]; } fl_uint_line_## n ##_t;
Comments
Post a Comment