C近拍令牌级联涉及的变量 - 这可能吗? [英] C Macro Token Concatenation involving a variable - is it possible?

查看:122
本文介绍了C近拍令牌级联涉及的变量 - 这可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图定义一个宏来生成令牌名称,包含一个变量。

I'm trying to define a macro to generate a token name, containing a variable.

基本上,我想是这样的:

Basically, what I'm trying is this:

#define GLUER(x,y,z) x##y##z
#define PxDIR(x) GLUER(P,x,DIR)

int main() {
  int port;
  port = 2;
  PxDIR(port) |= 0x01;
}

我希望能产生上述声明令牌P2DIR,但根据我的编译器的输出,它的生成令牌PportDIR,这不是我想要的东西。任何帮助这里?或者就是我试图做不可能的?

I'm hoping to generate the token P2DIR in the above statement, but according to my compiler output, it's generating the token PportDIR, which is NOT what I wanted. Any help here? Or is what I'm attempting to do impossible?

推荐答案

我不认为你想做什么是可能的。 C宏是真正的编译之前展开preprocessor宏。变量端口,不被设置到运行时。

I don't think what you're trying to do is possible. C macros are really preprocessor macros that are expanded before compilation. The variable port, doesn't get set until runtime.

这篇关于C近拍令牌级联涉及的变量 - 这可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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