How to compare and typecast function pointers in C?

Compare Two Function Pointers

Function pointer comparison becomes significant when we want to figure out if two function pointers are pointing to the same function. Function pointer comparison is important for one more use case, where we compare a function pointer with the NULL pointer to check if a function pointer is assigned any function name or not. There is no pointer arithmetic or relational operations are applied on function pointers because they would semantically serve no purpose. We see no use case of adding one function pointer to another or checking if one function pointer is less than the other.

Two function pointers can be compared with the == and != operators, just like any other kind of pointers. We can also compare a function pointer to the NULL pointer using the == and != operators. Function pointer comparisons with the <, <=, >, and >= operators yield undefined behaviour, if the two pointers are unequal.

Following C program demonstrates how to compare two function pointers if they are equal and unequal to each other.

#include <stdio.h>
 
int retMax (int n1, int n2)
{
  return (n1 > n2) ? n1 : n2;
}
 
int main ()
{
  //declare two function pointers
  int (*ptrMaxFunctin)(int, int);
  int (*ptrMaxFunctin2)(int, int);
 
  //assign function address to pointer
  ptrMaxFunctin = retMax; 
 
  // assign NULL to function pointer
  ptrMaxFunctin2 = NULL;
 
  if (ptrMaxFunctin2 == NULL)
  {
    printf("ptrMaxFunctin2 has not been assigned yet.\n");
  }
 
  if (ptrMaxFunctin2 != ptrMaxFunctin)
  {
   printf("ptrMaxFunctin2 and ptrMaxFunctin do not point to the same function.\n");
  }
  else
  {
     printf("ptrMaxFunctin2 and ptrMaxFunctin point to the same function.\n");
  }
 
  ptrMaxFunctin2 = retMax;
 
  if (ptrMaxFunctin2 == ptrMaxFunctin)
  {
    printf("ptrMaxFunctin2 and ptrMaxFunctin point to the same function.\n");
  }
  else
  {
    printf("ptrMaxFunctin2 and ptrMaxFunctin do not point to the same function.\n");
  }
 
  int qty1 = 20, qty2 = 50;
 
  printf ("Max of %d and %d is : %d \n", qty1, qty2, (*ptrMaxFunctin)(qty1, qty2));
  return 0;
}
 
OUTPUT
======
ptrMaxFunctin2 has not been assigned yet.
ptrMaxFunctin2 and ptrMaxFunctin do not point to the same function.
ptrMaxFunctin2 and ptrMaxFunctin point to the same function.
Max of 20 and 50 is : 50
 

Typecast Function Pointer to another Type

Can a function pointer store the address of any function? Yes, it can. This is purpose of casting function pointers, just like usual pointers.

We can cast a function pointer to another function pointer type but cannot call a function using casted pointer if the function pointer is not compatible with the function to be called.

If a function pointer is casted to a function pointer of a different type and called, it results into undefined behaviour. In other words the behaviour of a function pointer will be undefined if a function pointer is used to call a function whose type is not compatible with the pointed-to type.

As a conclusion, we have to cast a function pointer back to the original type in order to call a function, if we had casted a function pointer to a different function pointer type earlier.

Here, read more on Function Pointers in C.

Last Word

Hope you have enjoyed reading function pointer comparison and their type-casting. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!




Get Free Tutorials by Email

About the Author

is the main author for cs-fundamentals.com. He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.

Today's Tech News

WannaCry ransom notice analysis suggests Chinese linkPosted on Monday May 29, 2017

Researchers say the WannaCry ransom note was poorly translated - possibly using Google Translate.

BA boss 'won't resign' over flight chaosPosted on Monday May 29, 2017

Chief executive Alex Cruz says flight disruption at Heathrow and Gatwick had nothing to do with cost cutting.

Hay Festival 2017: Stephen Fry's warning for the webPosted on Sunday May 28, 2017

Delivering a lecture at the Hay Festival, the actor said society could face "dire consequences".

Courtesy BBC News