简体   繁体   中英

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

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM