Macro Concatenation

Discussion to talk about software related topics only.
Post Reply
SeeCwriter
Posts: 630
Joined: Mon May 12, 2008 10:55 am

Macro Concatenation

Post by SeeCwriter »

This may not be the right group, but I had a question about using macro concatenation feature of the compiler.

I'm using v2.7.3 on a Nano. This is the code:

Code: Select all

#define HEARTBEAT_PIN	12	// Heartbeat LED pin.
#define GPIO_FUNCTION(p)	(PIN_ ## p ## _GPIO)
...

Pins[HEARTBEAT_PIN].function( GPIO_FUNCTION( HEARTBEAT_PIN ) );

I get error 'PIN_HEARTBEAT_PIN_GPIO' not declared.

Why doesn't the 'p' in the macro get replaced with the number 12?
sulliwk06
Posts: 118
Joined: Tue Sep 17, 2013 7:14 am

Re: Macro Concatenation

Post by sulliwk06 »

I always find that the way some macros get resolved messes with my head. I think this may do what you're looking for:

Code: Select all

#define CONCATENATE_3(x,y,z) x##y##z
#define GPIO_FUNCTION(p) CONCATENATE_3(PIN_,p,_GPIO)
SeeCwriter
Posts: 630
Joined: Mon May 12, 2008 10:55 am

Re: Macro Concatenation

Post by SeeCwriter »

That works. Thank you.
Post Reply