C Syntax Pointers To Function

 

- Info:

- Pointer to Function is Pointer that holds address at which some Array is stored.

- Additional info can be found at http://www.newty.de/fpt/index.html.

 

- Create Pointer to Function:

- This example shows how to create pointer to function which returns float and takes two int parameters.

- Create C Console Application Test.c.

 

Test.c

#include <stdio.h>

float divide(int a, int b){

return (float) a/b;

}

int main () {

 

float (*ptf1)(int, int) = divide; /* Create pointer to function. Short way. */

float (*ptf2)(int, int) = &divide; /* Create pointer to function. */

float res1 = divide (5,2); /*Call function by name. */

float res2 = (&divide)(5,2); /*Call function by its address. */

float res3 = ptf1 (5,2); /*Call function by pointer to function. Short way.*/

float res4 = (*ptf1) (5,2); /*Call function by pointer to function. */

printf("%f %f %f %f", res1, res2, res3, res4);

return 1;

}