Macro concatenation inside another macro account in c

I have the following macros:

#define    __IR( x )        ICU.IR[ IR ## x ].BIT.IR
#define     _IR( x )        __IR( x )
#define      IR( x , y )    _IR( _ ## x ## _ ## y )

I use it as follows:

IR(SCI7, RXI7) = 0;

It expands to:

ICU.IR[ IR_SCI7_RXI7 ].BIT.IR = 0

Instead of using SCI7and RXI7I would like to use sci(channel)and rxi(channel). So I tried to create the following macros:

#define _sci(x)  SCI ## x
#define  sci(x)  _sci(x)
#define _rxi(x)  RXI ## x
#define  rxi(x)  _rxi(x)

#define channel 7

And then:

IR(sci(channel), rxi(channel)) = 0;

But that did not work. The compiler returns me:

Error [Pe017]: expected a "]"

I also tried with other manners, but to no avail.

What am I doing wrong?

+4
source share
2 answers

The entire macro is expanded using letter subexpressions, and the macros in the result expression are expanded after that.

So you can write:

#define    __IR(x )      ICU.IR[ IR ## x ].BIT.IR
#define     _IR(x, y)    __IR(_ ## x ## _ ## y)
#define      IR(x, y)    _IR(x, y)

#define _sci(x)  SCI ## x
#define  sci(x)  _sci(x)
#define _rxi(x)  RXI ## x
#define  rxi(x)  _rxi(x)

#define channel 7

IR(sci(channel), rxi(channel)) = 0;

(, , _IR. , , , , .)

+4

, , , .

IR(sci(channel), rxi(channel)) = 0 _IR( _sci(channel)_rxi(channel)) = 0 ICU.IR[IR_sci(channel)_rxi(channel)].BIT.IR = 0. , C.

C . , , .

. ? , - .

+2

Source: https://habr.com/ru/post/1584491/


All Articles