cat test_macro.c
#define TEST_MACRO(b) chip->##b
int main(void)
{
TEST_MACRO(yyy)
return 0;
}
gcc -E test_macro.c
# 1 "test_macro.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "test_macro.c"
int main(void)
{
test_macro.c:1:29: error: pasting "->" and "yyy" does not give a valid preprocessing token
#define TEST_MACRO(b) chip->##b
^
test_macro.c:5:9: note: in expansion of macro ‘TEST_MACRO’
TEST_MACRO(yyy)
^
chip->yyy
return 0;
}
What's pasting in above block ?
It is often useful to merge two tokens into one while expanding macros. This is called token pasting or token concatenation.
Reference :
3.5 Concatenation in [The C Preprocessor] for GCC version 8.1.0]
Question :
What's valid preprocessing token ?
preprocessing-token:
- head-name
- identifer
- pp-number
- character-constant
- string-literal
- punctuator
- each non-white-space character that cannot be one of the above
Error message mean that "->" is not a valid preprocessing token ?
or
"->" is not a valid preprocessing token for operator "##" ?
Solve :
cat test_macro.c
#define TEST_MACRO(b) chip->b
int main(void)
{
TEST_MACRO(yyy)
return 0;
}
gcc -E test_macro.c
# 1 "test_macro.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "test_macro.c"
int main(void)
{
chip->yyy
return 0;
}
Reference :
[To be continued]