How to concatenate strings with a C preprocessor with dots in them?

I read the following question, and the answer seems clear enough: How to combine twice with the C preprocessor and expand the macro, as in "arg ## _ ## MACRO",

But what if VARIABLE has a period at the end?

I am trying to make a simple macro that increments counters in a structure for debugging purposes. I can easily do this even without the help of the above question, simply by using

#ifdef DEBUG
#define DEBUG_INC_COUNTER(x) x++
#endif

and name him

DEBUG_INC_COUNT(debugObj.var1);

But the addition of "debugObj." for every macro it seems terribly redundant. However, if I try to combine:

#define VARIABLE debugObj.
#define PASTER(x,y) x ## y++
#define EVALUATOR(x,y)  PASTER(x,y)
#define DEBUG_INC_COUNTER(x) EVALUATOR(VARIABLE, x)
DEBUG_INC_COUNTER(var)

gcc -E macro.c

I get

macro.c:6:1: error: pasting "." and "var" does not give a valid preprocessing token

So how do I change this so that

DEBUG_INC_COUNTER(var);

generates

debugObj.var++;

?

+5
source share
2 answers

##; , . , (debugObj . var1 debugObj.var1).

+5

##, debugObj . var1 .

:

#define DEBUG_INC_COUNTER(x) debugObj.x++
+4

All Articles