w3resource

C Language: Function pointers in C with example

How do function pointers work in C?

In C, a function pointer is a variable that stores the address of a function.
A function pointer can be used to pass a function as an argument to another function, or to call a function dynamically.

Here is an example:

#include <stdio.h>
int add(int x, int y, int z) {
    return x + y +z;
}
int multiply(int x, int y, int z) {
    return x * y *z;
}
int main() {
    int x = 10, y = 20, z = 30;
    int (*func_ptr)(int, int, int); // function pointer variable declaration

    // assign the address of add function to function pointer
	func_ptr = &add; 
    // calling add function using function pointer
	printf("Addition: %d\n", (*func_ptr)(x, y, z)); 

    // assign the address of multiply function to function pointer
	func_ptr = &multiply; 
	// calling multiply function using function pointer
    printf("\nMultiplication: %d\n", (*func_ptr)(x, y, z)); 
    return 0;
}

Output:

Addition: 60

Multiplication: 6000

In the said example, we declare two functions add and multiply, which take three integer arguments and return an integer value. We then declare a function pointer variable func_ptr that can point to any function that has the same signature as add, and multiply.

In main() function, we assign the address of the add() function to func_ptr, and use the function pointer to call add with arguments x, y and z. Finally, we assign the address of the multiply() function to func_ptr, and use the function pointer to call multiply with arguments x, y and z.

Contribute your code and comments through Disqus.



Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

C Programming - How do you pass a function as a parameter in C?

Declaration

A prototype for a function which takes a function parameter looks like the following:

void func ( void (*f)(int) );

This states that the parameter f will be a pointer to a function which has a void return type and which takes a single int parameter. The following function (print) is an example of a function which could be passed to func as a parameter because it is the proper type:

void print ( int x ) {
  printf("%d\n", x);
}

Function Call

When calling a function with a function parameter, the value passed must be a pointer to a function. Use the function's name (without parentheses) for this:

func(print);

would call func, passing the print function to it.

Function Body

As with any parameter, func can now use the parameter's name in the function body to access the value of the parameter. Let's say that func will apply the function it is passed to the numbers 0-4. Consider, first, what the loop would look like to call print directly:

for ( int ctr = 0 ; ctr < 5 ; ctr++ ) {
  print(ctr);
}

Since func's parameter declaration says that f is the name for a pointer to the desired function, we recall first that if f is a pointer then *f is the thing that f points to (i.e. the function print in this case). As a result, just replace every occurrence of print in the loop above with *f:

void func ( void (*f)(int) ) {
  for ( int ctr = 0 ; ctr < 5 ; ctr++ ) {
    (*f)(ctr);
  }
}

Ref : https://bit.ly/3skw9Um