[DGD] #define peculiarity?

Kris Van Hees aedil at alchar.org
Sat Feb 28 17:55:37 CET 2004


On Sat, Feb 28, 2004 at 11:35:32AM -0500, Michael McKiel wrote:
> Thus my question was, why is this the case, or is there a way to concatenate
> a string in a #define, I seem to recall some trick done for a #define'd exit
> wrapper in C, but I don't recall how to do so, nor if it would even work in
> LPC.

The preprocessor isn't evaluating the definition string, because that's not its
job.  I stores the expansion along with the defined name, and wherever the name
is used. the expansion is inserted (with processing of the # and ## operators
of course).  So, when it contains an expression, that expression is NOT
evaluated until the compiler does so in the expanded code.

What you want to do *is* possible on the preprocessor level, though it isn't
really worth the trouble, usually.  With a smart combination of the # operator
(turn the 'argument' into a string), and the ## operator (concatenate two
literal strings) you can accomplish this.  You do have to build in some
intermediary defines to ensure that the arguments get evaluated properly since
preprocessor semantics on that are a bit odd if you're not used to it.  Consult
any good reference on the preprocessor to figure out how to do this.

One example would be:

#define _STR(x)		#x
#define STR(x)		_STR(x)

#define _CONCAT(x, y)	x##y
#define CONCAT(x, y)	_CONCAT(x, y)

#define KERNEL_DIR	/k
#define SYSTEM_DIR	/system/
#define USERD		STR(CONCAT(CONCAT(KERNEL_DIR, SYSTEM_DIR), userd))

	Kris
_________________________________________________________________
List config page:  http://list.imaginary.com/mailman/listinfo/dgd



More information about the DGD mailing list