宏观替代

ven*_*s.w 1 c linux macros

我写了以下代码:

#define GATE(name, num)do{\
  name##list[num] = #name;\
}while(0)

char* name[] = {"hello", "world", "byebye"};

int i;
for(i = 0; i < 3; i++)
{
    GATE(name[i],0);
}
Run Code Online (Sandbox Code Playgroud)

显然,我无法得到我想要的GATE(name[0],0)被替换的东西hellolist[0] = "hello",但是name[0]list[0] = "hello",这会导致gcc错误.

我想知道如何使用宏制作我想要的东西?

cni*_*tar 5

不,这不可能.预处理器在编译器启动之前进行文本替换.因此预处理器实际上从未看到字符串"hello" - 因此它不能发出hellolist.