Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Function Pointer

I'm a bit confused as to how I would pass a pointer to a pointer function. I have a function that takes a pointer to a function which I understand without problem (ExecBlock). But I'm given another prototype of a function (ExecBlock2) which takes the dereferenced-pointer (I'm not sure exactly what it is) and also takes the argument if passed function has any. If someone could explain the precedence and exactly what dereferencing a pointer function would do. Isn't that just passing the function itself?. What does (void *) do in this case?

int ExecBlock (void (*f) (void), int isSet)
{
    return ExecBlock2( HOW TO PASS HERE? , NULL, isSet);
}

int ExecBlock2(void *(*f)(void *), void *arg, int isSet)
{
    ... more code
}
like image 446
ThePedestrian Avatar asked Oct 17 '13 03:10

ThePedestrian


People also ask

How do you pass a function pointer?

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.

Can we pass pointer to function in C?

C programming allows passing a pointer to a function. To do so, simply declare the function parameter as a pointer type.

What is function pointer C++?

A function pointer is a variable that stores the address of a function that can later be called through that function pointer. This is useful because functions encapsulate behavior.

Which of the following can be passed to a function pointer?

which of the following can be passed in function pointers? Explanation: Only functions are passed in function pointers.


2 Answers

void (*f) (void)

means pointer to function with no arguments returning void.

void *(*f)(void *)

means pointer to function taking a void pointer and returning a void pointer.

Since the types are different, the compiler will not allow you to pass one to the other without casting. (Note that casting is not really the right answer here, and as @detly points out, results in undefined behavior.)

As for dereferencing pointers to functions, you don't have to explicitly put a "*" before a function pointer to call it. For instance, you could call your function pointer f just by doing

f();

A function pointer example

Say you have a function f, which you'd like to pass to a function called takes_a_function. takes_a_function will probably have a type like

void takes_a_function(void (*f)(void *data), void *data);

Notice how there are two arguments to takes_a_function, a function pointer, and a void pointer to some data. Also note that the function f happens to take a void pointer as an argument. The idea is that you can pass the data to takes_a_function, and it will pass it along to f. For example, takes_a_function could be defined like

void takes_a_function(void (*f)(void *), void *data) {
  f(data);
}

Now, let's write a function to pass to takes_a_function. Our function will just print an int that is passed to it.

void prints_an_int(void *data) {
  // The idiom for converting a void pointer to another kind
  // of pointer.  NO NEED TO CAST.  Note this behavior is only
  // defined if the pointer data really does point to an int.
  int *i = data;
  printf("%d", *i);
}

int i = 0;
takes_a_function(prints_an_int, &i);

A couple of key points about this example:

  • prints_an_int has the same type as the function pointer expected by takes_a_function. No need to cast.
  • There's no need to use the & operator to create a reference to a function. This is why we can pass prints_an_int to takes_a_function directly. But we could also say takes_a_function(&prints_an_int, &i), and it would be the same.
  • void* basically means "pointer to unknown type." To actually do anything with it, you must assign a variable of type void* to another pointer variable whose type you expect. This is only guaranteed to work if you actually pass in the correct pointer type! In this example, we can assign data to an int*, since data really does point to an int. If you want more data than just an integer, a common pattern is to create your own struct type which includes all the fields you want, and pass that instead.
  • As a special case, the compiler does not require you to cast when assigning void pointers to other pointers and vice-versa. But again, you only get defined behavior if you eventually convert a void pointer back to the correct type.
like image 111
Chris Rice Avatar answered Oct 17 '22 13:10

Chris Rice


For example you have a function

void * abc(void *)
{
//... 
}

int ExecBlock (void (*f) (void), int isSet)
    {
        return ExecBlock2( abc , NULL, isSet); //use name of the function which have void * as parameter type list and return type  void *   
        }

int ExecBlock2(void *(*f)(void *), void *arg, int isSet)
{
    ... more code
}  

See function-pointers

like image 1
Gangadhar Avatar answered Oct 17 '22 15:10

Gangadhar