Hi,

Need some help regarding the macro usage.

I've a case where the parameter to be passed to the macro function is not known till the point it is called.

example: (continuing the example quoted below)
- number "12" passed to the " DEFINE_FUNC( n )" macro is determined at the time of execution.

I cannot pass a string/int var from the macro
(i.e. i cannot use

int num = 12;
DEFINE_NUM(num);
)

Can you please help me and share any solution for such situation?

Thanks in advance!
Aditya


Actually, it is possible. You need to use token concatenation. #define DEFINE_FUNC( n ) void func_ ## n() You would use it in the normal way: DEFINE_FUNC( 12 ); The preprocessor would turn that into: void func_12(); While I can't imagine why you want to do this, there are, in fact, legitimate reasons to do token concatenation (which goes a long way into explaining why the preprocessor can do it at all).

Hope this helps.

> I've a case where the parameter to be passed to the macro function is not known till the point it is called.
Well you're out of luck then, because you can't generate C code at run-time.
The ## operator in the macro expansion is a compile-time only thing.

> Can you please help me and share any solution for such situation?
There isn't one.
You need to step back and explain the problem you're trying to solve in a more abstract way (so we can think of alternatives).

This article has been dead for over six months. Start a new discussion instead.