Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you have function pointer with assigned parameter value in C?

I want to point to something like add(2, 1) and then call it without needing to specify parameters. Is there some way to do this with function pointers?

like image 570
Etwus Avatar asked Mar 16 '18 16:03

Etwus


People also ask

Can we pass pointer to a function as parameter?

Pass-by-pointer means to pass a pointer argument in the calling function to the corresponding formal parameter of the called function. The called function can modify the value of the variable to which the pointer argument points. When you use pass-by-pointer, a copy of the pointer is passed to the function.

What Cannot be done with function pointer?

What will we not do with function pointers? Explanation: As it is used to execute a block of code, So we will not allocate or deallocate memory.

Can you pass functions as parameters in C?

We cannot pass the function as an argument to another function. But we can pass the reference of a function as a parameter by using a function pointer.

Can we have a pointer to a function in C?

In C, like normal data pointers (int *, char *, etc), we can have pointers to functions. Following is a simple example that shows declaration and function call using function pointer.


1 Answers

As a comment to your question, Basile Starynkevitch suggested reading about closures. At its core, a closure combines a function to be called with some context, referred to in the literature as the function’s environment.

Object-oriented languages have a similar concept known as delegates where a particular instance can become the environment for a later call where the calling site has no direct knowledge of the underlying object. Languages that support closures natively automatically capture or “close over” the bindings provided in the environment. It may seem an odd language feature, but it can be expressive and useful as the motivation behind your question suggests.

Below is a simple example of the concept of a closure at work in a C program, where it is up to the programmer to direct the captures explicitly.

The function you want to eventually call plus some front matter is

#include <stdio.h>

int
add_two_numbers(int a, int b)
{
  return a+b;
}

A closure is a function to be called combined with its environment. In C, the function to be called is a pointer to function. Note that the parameters of f align with those of add_two_numbers.

typedef struct {
  struct {  /* environment */
    int a;
    int b;
  } env;

  int (*f)(int, int);  /* function to be called */
} closure;

We’d like to create a closure, i.e., set up the association of parameter values to pass with the function to be called when we are ready to do so. In this simple example, make_adder leaves the problem of allocating space for the closure to its caller.

void
make_adder(int a, int b, closure *c)
{
  c->env.a = a;
  c->env.b = b;
  c->f = add_two_numbers;
}

Now that you know how to create one of our simple closures, you call or invoke it as in

int invoke_closure(closure *c)
{
  return c->f(c->env.a, c->env.b);
}

Finally, usage will look like

int main(void)
{
  closure c;
  make_adder(2, 1, &c);

  printf("The answer is %d.\n", invoke_closure(&c));

  return 0;
}

with output of

The answer is 3.

Further Reading

  • Currying
  • Partial Application
like image 200
Greg Bacon Avatar answered Oct 14 '22 13:10

Greg Bacon