0

I am trying to create a function:

int func(int a, int b) { if (a == b) return 1; else return 0; }

Then I would like to create a function pointer like:

int (*ptr)(int, int);

How can I assign dynamically ptr to allow for it to hold, for example, 10 different function calls?

ptr[0] = func(1, 1);
ptr[1] = func(1, 0);
...
ptr[9] = func(0, 0);

So that I can call function inside FOR loop via pointer like:

int result = 0;
for (int i = 0; i < 10; i++)
    result += ptr[i];
printf ("%d\n", result);
Amaterastis
  • 477
  • 3
  • 12
  • I would actually like to do that with dynamical allocation to the Function Pointer. – Amaterastis Nov 23 '19 at 12:51
  • Your question is unclear. You ask for “10 space for ptr”. Does that mean you want 10 pointers? Or does it mean you want 10 elements in an array named `ptr`? Are the elements intended to be pointers or to be `int` objects? As you have shown how `ptr` is used, it is merely an array of 10 `int`. That can be defined simply with `int ptr[10];`. Then, once `ptr` is created, how do you want to assign values to it? The sample values you show, with `func(1, 1)`, `func(1, 0)`, and `func(0, 0)` do not show a clear pattern. – Eric Postpischil Nov 23 '19 at 12:52
  • There is no need to use a function pointer (a pointer to a function) if all you are going to do is record the results of calling a function. Function pointers are generally used to point to various different functions, not to point to one function. Why do you want a function pointer? – Eric Postpischil Nov 23 '19 at 12:53
  • @EricPostpischil, I want it just because I am trying to learn more about it. :) – Amaterastis Nov 23 '19 at 12:54
  • You still need to tell us clearly what you want. Answer the questions asked in the first comment. You need to explain what type you want `ptr` to be, and you need to explain the pattern in the `func` calls. – Eric Postpischil Nov 23 '19 at 12:56
  • I've edited the question. I hope it is more clearer now? – Amaterastis Nov 23 '19 at 12:58
  • C does not have anything that can hold a “function call”. Calling a function is an action, not a value. Objects in C can represents values like integers, floating-point numbers, or pointers. Pointers can point to functions or objects. They cannot point to function calls. You can make a pointer point to `func`, and you can later use that pointer to call the function. You can make an `int` to hold the result of calling `func` with arguments 0 and 1, for example. You cannot make a pointer that holds a function call. – Eric Postpischil Nov 23 '19 at 13:01
  • 1
    Maybe you want something like `struct call_s { int arg1; int arg2; int (*func)(int, int); };` then `struct call_s calls[3] = { {1, 1, func}, {1, 0, func}, {0, 0, func} };` and then `for (...) { result += calls[i].func(calls[i].arg1, calls[i].arg2); }`? C doesn't have lambdas nor std::bind from C++. – KamilCuk Nov 23 '19 at 13:10

1 Answers1

0

Fucntion pointers hold function addresses, not whole unmade function calls.

With

int func(int a, int b) { if (a == b) return 1; else return 0; }
int (*ptr)(int, int) = &func;

You can use

(*ptr)(1,2) //call a function through a function pointer

instead of

func(1,2) //call a function directly

.

(Technically, standardized C also allows ptr(1,2), or (*func)(1,2), or abominations such as (***&***&*ptr)(1,2), because regular function calls are defined in terms of decay to function pointers.)

To store an unmade function call, you need both the function address and the parameters:

struct callToIntIntInt{ int (*fn)(int, int); int arg1, arg2; };
struct callToIntIntInt calls[10];
calls[0] = (struct callToIntIntInt){ &func, 1, 1 };
calls[1] = (struct callToIntIntInt){ &func, 1, 0 };
/*...*/
calls[9] = (struct callToIntIntInt){ &func, 1, 0 };

/*...*/
int result = 0;
for (int i = 0; i < 10; i++)
    result += calls[i].fn(calls[i].arg1, calls[i].arg2);
printf ("%d\n", result);

If the function pointers are all the same then of course storing them is a waste of space:

struct argsToIntIntInt{ int arg1, arg2; };
/*...*/
result += func(args[i].arg1, args[i].arg2);

With the syntax you proposed:

ptr[0] = func(1, 1);
ptr[1] = func(1, 0);
...
ptr[9] = func(0, 0);

you'd be making the function calls right then and there, the result would be int (the return type of func), and in that case, ptr could just be int ptr[10]; (array of ten ints–no function pointers).

Petr Skocik
  • 58,047
  • 6
  • 95
  • 142