In a #define clause, how to make the preprocessor replace a parameter inside a variable name?

懵懂的女人 提交于 2019-12-02 06:48:34

问题


I have the following code:

#define MY_MACRO(PARAM) int PARAM_int; double PARAM_double; [subsequent instructions]

Unfortunately, it does not work, meaning that PARAM is not replaced inside the variables names. Is this solvable some way?


回答1:


PARAM_int is considered to be a single token, that is distinct from PARAM. You can concatenate tokens in a macro definition with ##:

#define MY_MACRO(PARAM) int PARAM ## _int; double PARAM ## _double;

Now, PARAM will expand to whatever you invoke the macro with, and then the resulting token will be pasted together with _int and _double.



来源:https://stackoverflow.com/questions/27927988/in-a-define-clause-how-to-make-the-preprocessor-replace-a-parameter-inside-a-v

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!