Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function pointer as an argument

Is it possible to pass a function pointer as an argument to a function in C?

If so, how would I declare and define a function which takes a function pointer as an argument?

like image 343
inquisitive Avatar asked Nov 24 '09 12:11

inquisitive


People also ask

Can we pass pointer as an argument to function?

Just like any other argument, pointers can also be passed to a function as an argument.

How can we pass a pointer as an argument to a function explain with example?

Example 2: Passing Pointers to Functions Here, the value stored at p , *p , is 10 initially. We then passed the pointer p to the addOne() function. The ptr pointer gets this address in the addOne() function. Inside the function, we increased the value stored at ptr by 1 using (*ptr)++; .

How is a function pointer used?

A pointer to a function points to the address of the executable code of the function. You can use pointers to call functions and to pass functions as arguments to other functions. You cannot perform pointer arithmetic on pointers to functions.


5 Answers

Definitely.

void f(void (*a)()) {
    a();
}

void test() {
    printf("hello world\n");
}

int main() {
     f(&test);
     return 0;
}
like image 199
mmx Avatar answered Oct 05 '22 21:10

mmx


Let say you have function

int func(int a, float b);

So pointer to it will be

int (*func_pointer)(int, float);

So than you could use it like this

  func_pointer = func;
  (*func_pointer)(1, 1.0);

  /*below also works*/
  func_pointer(1, 1.0);

To avoid specifying full pointer type every time you need it you coud typedef it

typedef int (*FUNC_PTR)(int, float);

and than use like any other type

void executor(FUNC_PTR func)
{ 
   func(1, 1.0);
}

int silly_func(int a, float b)
{ 
  //do some stuff
}

main()
{
  FUNC_PTR ptr;
  ptr = silly_func;
  executor(ptr); 
  /* this should also wotk */
  executor(silly_func)
}

I suggest looking at the world-famous C faqs.

like image 27
Michal Sznajder Avatar answered Oct 05 '22 20:10

Michal Sznajder


This is a good example :

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

int mul(int a, int b)
{
   return a * b;
}

int div(int a, int b)
{
   return a / b;
}

int mathOp(int (*OpType)(int, int), int a, int b)
{
   return OpType(a, b);
}

int main()
{

   printf("%i,%i", mathOp(sum, 10, 12), mathOp(div, 10, 2));
   return 0;
}
The output is : '22, 5'

As said by other answers, you can do it as in

void qsort(void *base, size_t nmemb, size_t size,
           int (*compar)(const void *, const void *));

However, there is one special case for declaring an argument of function pointer type: if an argument has the function type, it will be converted to a pointer to the function type, just like arrays are converted to pointers in parameter lists, so the former can also be written as

void qsort(void *base, size_t nmemb, size_t size,
           int compar(const void *, const void *));

Naturally this applies to only parameters, as outside a parameter list int compar(const void *, const void *); would declare a function.


Check qsort()

void qsort(void *base, size_t nmemb, size_t size,
           int (*compar)(const void *, const void *));

The last argument to the function is a function pointer. When you call qsort() in a program of yours, the execution "goes into the library" and "steps back into your own code" through the use of that pointer.

like image 40
pmg Avatar answered Oct 05 '22 22:10

pmg