在#define子句中,如何使预处理器替换变量名称中的参数? [英] In a #define clause, how to make the preprocessor replace a parameter inside a variable name?

查看:59
本文介绍了在#define子句中,如何使预处理器替换变量名称中的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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

不幸的是,它不起作用,这意味着未在变量名称内替换PARAM.这是可以解决的一些方法?

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

推荐答案

PARAM_int被认为是单个令牌,与PARAM不同.可以在与宏定义置标记:

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.

这篇关于在#define子句中,如何使预处理器替换变量名称中的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆