宏作为另一个宏的参数 [英] Macro as a parameter to another macro

查看:186
本文介绍了宏作为另一个宏的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用另一个预定义的宏将参数传递给宏 SETBIT

I'm trying to pass the parameters to macro SETBIT with another predefined macro like this:

#define SETBIT(ADDRESS,BIT,N) {(N) ? (ADDRESS &= ~(1<<BIT)) : (ADDRESS |= (1<<BIT))}
#define DAC_SYNC PORTB,3,POS
SETBIT(DAC_SYNC);

但是我收到错误:


宏SETBIT仅需要3个参数

macro SETBIT requires 3 parameters only 1 given

有一个文章,其中包含以下建议:

There is an article with the following recommendations:

到防止误算术运算: #define foo(a,b) #define bar(x)loss((x))

但是即使我仍然有错误。顺便说一句,阅读我已经指出的文章,我可以得出以下结论:预处理程序扩展出现的所有宏。但是实际上看起来像宏 #define DAC_SYNC PORTB,3,POS 不会被预处理程序扩展。

But even though I still have an error. BTW, reading the article I've indicated I can make the following conclusion: preprocessor expands ALL macroses appearing. But actually it looks like macro #define DAC_SYNC PORTB,3,POS is not expanding by preprocessor.

任何人都可以

推荐答案

这有效:

#define SETBIT2(ADDRESS,BIT,N) ((N) ? (ADDRESS &= ~(1<<BIT)) : (ADDRESS |= (1<<BIT)))
#define SETBIT(PARAMS) SETBIT2(PARAMS)
#define PORTB 5
#define POS 7
#define DAC_SYNC PORTB,3,POS

int main() {
  int a = SETBIT(DAC_SYNC);
  return 0;
}

这篇关于宏作为另一个宏的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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