Pointers to Functions | Pointer with Functions in C

As we know that in c language a function is not a variable, but it still has a type and a phsical address location in memory. This address is the entry point of the function and it is the address used when the function is called.

Now therefore it  is possible to declare a pointer to a function, which can then be used as an argument in another function. Once a pointer points to a function, the function can be called through that pointer. The function pointers also allow functions to be passed as arguments to other functions.

A pointer to a function is declared as follows :
 type (*fptr) ();
Where fptr is a pointer to a function or function pointer and type specifies the return type value. Also note that at above declaration, the parentheses around *fptr are necessary.

In C language, we can make a function pointer by simply assigning the name of the function to the pointer. Now lets see an example, suppose we have function named numChk() which takes two integer numbers as arguments and returns the bigger one.
int numChk(int x, int y) {
  return(x > y ? x : y);
}
The below statement create a function pointer to the function numChk()
 int (*ptr)();
 ptr = numChk;
Where ptr is pointer to the function numChk(). Now in order to call the function numChk() through function pointer, we have to use the pointer ptr with the list of parameters. That is :
 (*ptr)(x, y);
The above statement is equivalent to :
 numChk(x, y);
Now lets see the C example Code :
#include <stdio.h>

int numChk(int x, int y) {
  return(x > y ? x : y);
}

int main() {
  int a,b;

  int (*ptr)();
  ptr = numChk;

  printf("Enter two numbers :\n");
  scanf("%d", &a);
  scanf("%d", &b);

  printf("%d is the big number.\n", (*ptr)(a,b));
  
  return 0;
}
Output :


Enter two numbers :
20
25

25 is the big number.

Note that at the function call the parentheses around the function pointer is necessary {(*ptr) at line 17} for the compiler to interpret this statement correctly. Now lets see another example :
#include <stdio.h>

int numChk(int x, int y) {
  return(x > y ? x : y);
}

void check(int a, int b, int(*fptr)(int, int)) {
  printf("\n%d is the big number.\n", (*fptr)(a, b));
}

int main() {
  int a,b;
 
  int (*ptr)();
  ptr = numChk;

  printf("Enter two numbers :\n");
  scanf("%d", &a);
  scanf("%d", &b);
  check(a, b, ptr);
  return 0;
}
Output :

Enter two numbers :
200
210

210 is the big number.

At the above c program we have a function check() which calls the function numChk(), through the function pointer ptr.
void check(int a, int b, int(*fptr)(int, int)) {
  printf("\n%d is the big number.\n", (*fptr)(a, b));
}
At the function declaration, the third argument is for the function pointer,
 int(*fptr)(int, int) 
Where the two int in bracket specifies the two integer arguments of function numChk(). In main function the check() function is called with two integer variables (a,b) and one function pointer (ptr) as a parameters
 check(a, b, ptr);
The function pointer is a confusing yet powerful feature of C language. A programmer may wonder why anyone would write a program in this way. However, some times it is advantageous to pass functions as parameters or to create an array of functions. For example when a compiler or interpreter is written, the parser (the part that evaluates expressions) often calls various support functions, such as those that compute mathematical operations (sine, cosine, tangent, etc.), perform I/O, or access system resources. Instead of having a large switch statement with all of these functions listed in it, an array of functions is selected by its index.

Let see another example :
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

void check(char *a, char *b, int(*cmp)(const char *, const char *)) {
  printf("Testing for equality.\n");
  if(!(*cmp)(a, b))
    printf(":> str1 and str2 are Equal\n");
  else
    printf(":> str1 and str2 are Not Equal\n");
}   

int numcmp(const char *a, const char *b) {
  if(atoi(a) == atoi(b))
    return 0;
  else
    return 1;
}

int main() {
  char str1[50], str2[50];
  printf("Enter some strings :\n");
  printf("str1 : ");
  gets(str1);
  printf("str2 : ");
  gets(str2);

  if(isalpha(*str1))
    check(str1, str2, strcmp);
  else    
    check(str1, str2, numcmp);

  return 0;
}
Output :

Enter some strings :
str1 : hello world
str2 : hello world
Testing for equality.
:> str1 and str2 are Equal


At the above program, if you enter a letter, strcmp() is passed to check(). Otherwise numcmp() is used. For example :

Output :

Enter some strings :
str1 : 123
str2 : 123
Testing for equality.
:> str1 and str2 are Equal

Since check() calls the function that it is passed, it can use different comparison functions in different cases.

Next Topic :