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).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.