0

I have a quastion about C functions. Is there any possibility to do something like:

#define PRINT_SUM_OF_CONSTS()     printSum(10, 5)

void printSum(int a, int b){
    print("%d + %d = %d", a, b, a+b);
}

int main(){
    void (*pFunc)(void);
    pFunc = &PRINT_SUM_OF_CONSTS;
    pFunc();
    return 0;
}

What I need is to use function which takes two arguments, asign arguments to constants with a macro and use it as function without arguments. Is that somehow possible?

  • 1
    What you're asking about is either [currying](https://en.wikipedia.org/wiki/Currying) or [partial application](https://en.wikipedia.org/wiki/Partial_application). Does this answer your question? [Is there a way to do currying in C?](https://stackoverflow.com/questions/1023261/is-there-a-way-to-do-currying-in-c) – Brian61354270 Apr 12 '21 at 15:49
  • You need `pFunc` to point to a function with a prototype that matches. You could use a wrapper function, such as `void print_sum_of_consts(void) { printSum(10, 5); }` and then assign `pFunc = print_sum_of_consts;`. (You could replace `printSum(10, 5)` with `PRINT_SUM_OF_CONSTS()` in the above.) – Ian Abbott Apr 12 '21 at 15:51
  • 1
    Perhaps a better duplicate target would be [emulating partial function application in c](https://stackoverflow.com/questions/5524299/emulating-partial-function-application-in-c) or [Change signature of function pointer in C when one argument is constant](https://stackoverflow.com/questions/47817243/change-signature-of-function-pointer-in-c-when-one-argument-is-constant?noredirect=1&lq=1) – Brian61354270 Apr 12 '21 at 15:53
  • Short answer; No Longer answer: You can do a number of tricks to make it work but it can't be done directly using constants – Support Ukraine Apr 12 '21 at 16:10
  • You are making a simple thing complicated. Why not simply use a wrapper function rather then a macro (which wont work in any case)? – Clifford Apr 12 '21 at 16:52
  • Do you really need the function pointer? As opposed to just using the wrapper macro directly? There are reasons why you might indeed need the pointer, but the example code does not make it clear that any of those apply. – John Bollinger Apr 12 '21 at 16:56
  • Ok, I got this, wrapper function is ok. But I was looking for little bit clear solution. My problem is that I use one function with lets say 8 combinations of arguments and pass it as function argument to more complex function through a pointer. So I wanted to know if I can save some memory (ok it wont take so much memory :), maybe just skip one unnecessary function call) and make it little more clear with just one function and a couple of different macros. So the answer according to all comments - NO i cant. Thank you – Daniel Šebík Apr 12 '21 at 18:39

1 Answers1

2

pFunc is a pointer to a function, you cannot create a pointer to a function call with specific parameters.

However, the solution is simple and does not require a macro - you simply create a wrapper function that calls the target function with the desired parameters:

#include <stdio.h>

void printSum(int a, int b)
{
    printf("%d + %d = %d", a, b, a+b);
}

void printSumConst() { printSum(10, 5) ; }


int main()
{
    void (*pFunc)(void) = printSumConst;
    pFunc();
    return 0;
}
Clifford
  • 88,407
  • 13
  • 85
  • 165
  • Ok, I got this. But I wanted to know if there is another solution. My problem is that I use one function with lets say 8 combinations of arguments and pass it as function argument to more complex function through a pointer. So I wanted to know if I can save some memory and make it little more clear with just one function and a couple of different macros. So the answer according to all comments - NO i cant. Thank you – Daniel Šebík Apr 12 '21 at 18:36
  • @DanielŠebík : Macros seldom make anything clearer. Your macro was one line, the wrapper function is one line - it is not more complex and is valid. Even if your solution worked, I doubt it would save a significant amount of memory if at all. Often macros result in more code generation because of code duplication. – Clifford Apr 12 '21 at 19:41