Asked in
C Programming

What does the C statement int open parenthesis asterisk f close parenthesis open parenthesis int asterisk close parenthesis declare?


User Avatar
Wiki User
June 02, 2016 6:30PM

The declaration int (*f) (int*); declares a function pointer named f. The function pointer can be assigned the address of any function that accepts a pointer to int and returns an int. Function pointers can be used to pass functions to functions. Normally we use typedefs to simplify the notation of function pointers:

typedef int (*f) (int*);

int x (int*);

int y (int*);

f fp; // declare a function pointer of type f

int z = 42;

fp = x; // point to the x function

fp (&z); // invoke function via pointer

fp = y; // point to the y function

fp (&z); // invoke function via pointer.

A typical usage of function pointers is to provide a predicate for a comparison sort algorithm. This makes it possible for the same sorting algorithm to compare objects using different predicates. For example:

typedef bool (*pred) (int, int); // function pointer type named pred

void sort (int a[], size_t len, pred func) {

// simple shell sort

for(int i=len/2; i>0; i=i/2)


for(int j=i; j<len; j++)


for(k=j-i; k>=0; k=k-i)


if( !func (a[k+i], a[k]) // invoke the predicate function


swap (a[k], a[k+i]);






// Declare predicates...

bool less_than (int a, int b) { return a<b);

bool greater_than (int a, int b) { return a>b; }

int main () {

int x[] = {3,5,2,4,1};

sort (x, 5, less_than); // sort array of 5 elements in ascending order

sort (x, 5, greater_than); // sort array of 5 elements in descending order

return 0;