C语言宏定义的##使用问题
今天学习宏定义,使用##时遇见了下面的问题
- C/C++ code
#include<stdio.h>#include<string.h>#define PI 3.14#define AREA(R) PI*(R)*(R)#define T(sth) #sth#define STR(X) love##Xint main(int argc,char **argv){ printf("PI=%f\n",PI); printf("area=%f\n",AREA(10)); printf("TTTT=%s\n",T(NIHAO));// char *love=STR(111); char *love; strcpy(love,STR(111)); printf("STR=%s\n",love); return 0;}上面的代码中只有STR(111)时出现了问题
- C/C++ code
[root@bogon c_study]# gcc -o hong宏 -g hong宏.c hong宏.c: In function ‘main’:hong宏.c:22: error: ‘love111’ undeclared (first use in this function)hong宏.c:22: error: (Each undeclared identifier is reported only oncehong宏.c:22: error: for each function it appears in.)
怎么使用这个##呢?在网上看了很多,但是没有多少很详细的说的!
请问上面额问题怎么解决
[解决办法]
##只是简单将左右两侧 结合 成新的标识符
STR(111)经过预处理之后展开为love111,注意love111都没有数据类型,如何strcpy
还有strcpy(love,STR(111));love没有指向有效内存,用法不对
[解决办法]
gcc展开宏的参数好象是-E
[解决办法]
#define STR(X) T(love##X)