ho letto alla seguente domanda e la risposta sembra abbastanza chiara: How to concatenate twice with the C preprocessor and expand a macro as in "arg ## _ ## MACRO"?Come concatenare stringhe con il preprocessore C con punti in esse?
Ma cosa succede se variabile ha un punto alla fine?
Sto provando a fare una semplice macro che incrementa i contatori in una struttura a scopo di debug. Posso facilmente farlo anche senza l'aiuto della domanda di cui sopra semplicemente con
#ifdef DEBUG
#define DEBUG_INC_COUNTER(x) x++
#endif
e chiamarlo
DEBUG_INC_COUNT(debugObj.var1);
Ma l'aggiunta di "debugObj." ad ogni macro sembra terribilmente ridondante. Tuttavia se provo per concatenare:
#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
ottengo
macro.c:6:1: error: pasting "." and "var" does not give a valid preprocessing token
Così come dovrei cambiare questo in modo che
DEBUG_INC_COUNTER(var);
genera
debugObj.var++;
?
D'oh! Sono diventato troppo intelligente lì. – Makis