Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C Macro Token Concatenation involving a variable - is it possible?

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;
}

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?

like image 464
gatesphere Avatar asked Sep 03 '10 04:09

gatesphere


1 Answers

... or just make PORT also a macro:

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

int main() {
    PxDIR(PORT) |= 0x01;
    return 0;
}
like image 102
Agnius Vasiliauskas Avatar answered Oct 27 '22 02:10

Agnius Vasiliauskas