简体   繁体   中英

How to Concat Strings using macro or any other ways

I have similar macros defined with just difference in them is number. eg

#define Function_01_Call(param)  (FunctionName((int)01, param))
#define Function_02_Call(param)  (FunctionName((int)02, param))
#define Function_03_Call(param)  (FunctionName((int)03, param))
#define Function_04_Call(param)  (FunctionName((int)04, param))

I want to call function FunctionName using macros Function_XX_Call . How can I use one string for macro with change in its numbers? I tried with

#define FUNCTION_CALL(num)   Function_num_Call

 int main()
 {
    char num;
    for(num = "01"; num<="04"; num++)
    {
       FUNCTION_CALL(num); //HOW TO PASS param HERE?
    }
  }

but how can I change the num dynamically during call as Variables cannot be used in macro. Also how to pass the param during call? Is there any way to use function pointers?

how can I change the num dynamically

The best is to create callers and a lookup table. So completely ignore the whole processor stuff anything there is and move to runtime.

void function_call_01(int param) { Function_01_Call(param); }
void function_call_02(int param) { Function_02_Call(param); }
void function_call_03(int param) { Function_03_Call(param); }
void function_call_04(int param) { Function_04_Call(param); }

static void (*const function_calls[])(int) {
    function_call_01,
    function_call_02,
    function_call_03,
    function_call_04,
};
static const function_calls_cnt = sizeof(function_calls)/sizeof(*function_calls);

void function_call(size_t idx, int param) {
   assert(idx < function_calls_cnt);
   function_calls[idx](param);
}

int main() {
    for (size_t i = 0; i < function_calls_cnt; ++i) {
         function_call(i, 62);
    }
}

Preprocessor is static, it can't be changed after compilation. If you want anything to depend on something on runtime, you have to write it in runtime, not in preprocessor.

The function list and array definition could be "shortened" with FOREACH macros, like P99_SEQ or BOOST_PP_SEQ_FOR_EACH or similar.

You can't!

Because the for loop will be executed at runtime, but the macros are expanded in the pre-processor phase, even before compiling. Obviously the value of num is unknown and can't be expanded in the macro at that time.

for(num = 1; num<=4; num++)
{
   FUNCTION_CALL(num); //HOW TO PASS param HERE?
}

Btw. You can't store a two character string in a char

You CAN do something like

#define __PASTE1(a,b,c)  __PASTE2(a,b,c)
#define __PASTE2(a,b,c)  a##b##c
#define FUNCTION_CALL(num,param)   __PASTE1(Function_,num,_Call)(param)

int main()
{
  FUNCTION_CALL(01); // Expands to Function_01_Call(param)
  FUNCTION_CALL(02);
  FUNCTION_CALL(03);
  FUNCTION_CALL(04); // Expands to Function_04_Call(param)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM