Preprocessing

R

R.Biloti

Hi folks

I would like to do something like

#define data_x1 data_x1_bbj
#define data_x2 data_x2_bbj
#define label_xy label_xy_bbj

Since the suffix bbj can change from time to time, I've tried

#define XX bbj

#define data_x1 data_x1_ ## XX
#define data_x2 data_x2_ ## XX
#define label_xy label_xy_ ## XX

but that doesn't produce what I'm willing, that is, XX doesn't get
replaced
by bbj in the three define's.

Any clue how to do that?

Regards,
Biloti
 
M

Mohan

R.Biloti said:
Hi folks

I would like to do something like

#define data_x1 data_x1_bbj
#define data_x2 data_x2_bbj
#define label_xy label_xy_bbj

Since the suffix bbj can change from time to time, I've tried

#define XX bbj

#define data_x1 data_x1_ ## XX
#define data_x2 data_x2_ ## XX
#define label_xy label_xy_ ## XX

but that doesn't produce what I'm willing, that is, XX doesn't get
replaced
by bbj in the three define's.

Any clue how to do that?

you may want to do it like this:

#define DATA_X1(a) data_x1 ## a
#define DATA_X2(a) data_x2 ## a
#define DATA_XY(a) data_xy ## a

and you can use it as - DATA_X1(bbj), DATA_X2(obj), etc

--Mohan
 
R

R.Biloti

Mohan escreveu:
you may want to do it like this:

#define DATA_X1(a) data_x1 ## a
#define DATA_X2(a) data_x2 ## a
#define DATA_XY(a) data_xy ## a

and you can use it as - DATA_X1(bbj), DATA_X2(obj), etc

--Mohan

But that implies that I should type bbj wherever I use the macros
DATA_X1, DATA_X2, etc, and therefore, when I change bbj to kkt, I would
have to search-and-replace in the entire code, which is exactly what I
as trying to avoid.

Biloti
 
A

Allan M. Bruce

R.Biloti said:
Mohan escreveu:


But that implies that I should type bbj wherever I use the macros
DATA_X1, DATA_X2, etc, and therefore, when I change bbj to kkt, I would
have to search-and-replace in the entire code, which is exactly what I
as trying to avoid.

Biloti

or you could have another macro
#define SUFF bbj

and use DATA_X1(SUFF), DATA_X2(SUFF) then all you need to do is change the
define for SUFF to avoid searching through the whole code
Allan
 
M

Mohan

R.Biloti said:
Mohan escreveu:


But that implies that I should type bbj wherever I use the macros
DATA_X1, DATA_X2, etc, and therefore, when I change bbj to kkt, I would
have to search-and-replace in the entire code, which is exactly what I
as trying to avoid.

No, you need to have one more set of macros, like (can be placed in a
header file):
#define DATA_X1(a) data_x1 ## a
#define DDATA_X1 DATA_X1(obj)

#define DATA_X2(a) data_x2 ## a
#define DDATA_X2 DATA_X2(obj)

etc

the macros DDATA_* are used in the rest of the code, hence the changes
are centralized.

--Mohan
 
S

SM Ryan

# Hi folks
#
# I would like to do something like
#
# #define data_x1 data_x1_bbj
# #define data_x2 data_x2_bbj
# #define label_xy label_xy_bbj
#
# Since the suffix bbj can change from time to time, I've tried
#
# #define XX bbj
#
# #define data_x1 data_x1_ ## XX
# #define data_x2 data_x2_ ## XX
# #define label_xy label_xy_ ## XX
#
# but that doesn't produce what I'm willing, that is, XX doesn't get
# replaced
# by bbj in the three define's.
#
# Any clue how to do that?

You have to #define two levels down.

t.c:
#define data_x1 CONCAT(data_x1_,XX)
#define data_x2 CONCAT(data_x2_,XX)
#define label_xy CONCAT(label_xy_,XX)

#define XX bbj

#define CONCAT(a,b) CONCAT2(a,b)
#define CONCAT2(a,b) a##b

x1 = data_x1
x2 = data_x2
xy = label_xy

cc -E t.c
# 1 "t.c"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "t.c"
# 10 "t.c"
x1 = data_x1_bbj
x2 = data_x2_bbj
xy = label_xy_bbj
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top