1

I have a series of functions that I need to call, and all the calls are of the form:

StartingFunctionName[Thing]Callback(&[Thing]);

Where [Thing] is the same thing each time for example, I have to call

StartingFunctionNameMyFunctionCallback(&MyFunction);

and I'd like to rather do foo(MyFunction); where foo would be the macro.

I was wondering if there was a way to use a macro, so that its input is the string (or something like that) Thing, and the output is the correct line of code.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • It's a bit hard to follow. Are you saying that the string contains the name of the function you want to call (or is part of the name of the function)? – john Nov 15 '22 at 23:15
  • Does this answer your question? [How can I concatenate twice with the C preprocessor and expand a macro as in "arg ## \_ ## MACRO"?](https://stackoverflow.com/questions/1489932/how-can-i-concatenate-twice-with-the-c-preprocessor-and-expand-a-macro-as-in-ar) – dewaffled Nov 15 '22 at 23:20
  • I tried to clarify my question, sorry if that wasn't clear. – Guelque Hadrien Nov 15 '22 at 23:21
  • I'd probably write a function (which can be inlined) instead of a macro. Bit hard to know from your example (too many of the parts of your example are unspecified). – Peter Nov 15 '22 at 23:21

2 Answers2

2

If this is your pattern:

StartingFunctionName[Thing]Callback(&[Thing]);

and you want the token 'Thing' replaced, then via function like macro:

#define foo(Thing) \
    StartingFunctionName##Thing##Callback(&Thing)

Example:

foo(exit);

expands to

StartingFunctionNameexitCallback(&exit);
Erdal Küçük
  • 4,810
  • 1
  • 6
  • 11
1

The answer was to use the ## #define foo(thing) StartingFunctionName##thing(& thing)