Reputation: 175
I'm getting an error when I try to use ## in macro this is what I try to make:
With this defines:
#define PORT 2
#define PIN 3
I want that preprocessor generates:
PM2.3=1
when I call a macro like this:
SetPort(PORT,PIN)
Then, I see that I can make the substitution PORT and PIN at the same time that concatenation, then I think I must to use 2 defines:
#define SetP2(PORT,PIN) PM##PORT.PIN = 1
#define SetPort(PORT,PIN) SetP2(PORT,PIN)
but I get an error on:
#define PIN 3 --> expected identifier before numeric constant
and a warning on:
SetPort(PORT,PIN) --> Syntax error
Any idea?
Upvotes: 1
Views: 538
Reputation: 15996
This works for me:
$ cat portpin.c
#define PORT 2
#define PIN 3
#define SetP2(prefix,prt) prefix ## prt
#define SetPort(prt,pn) SetP2(PM,prt).pn = 1
SetPort(PORT,PIN)
$ gcc -E portpin.c
# 1 "portpin.c"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "portpin.c"
PM2. 3 = 1
$
I don't know how important it is for there to be no space between the .
and the 3
, but the preprocessor seems to want to insert it.
UPDATE:
Actually I tried your original code, and it seems to produce the same result, so my answer above is probably not much use to you.
UPDATE 2:
It turns out the OP is expecting the pre-processor to generate PM2.no3=1
and not PM2.3=1
. This can easily be done as follows:
$ cat portpin.c
#define PORT 2
#define PIN 3
#define SetP2(PORT,PIN) PM##PORT.no##PIN=1
#define SetPort(PORT,PIN) SetP2(PORT,PIN)
SetPort(PORT,PIN)
$ gcc -E portpin.c
# 1 "portpin.c"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "portpin.c"
PM2.no3=1
$
Upvotes: 1