Get Pointers to Functions - C Pointer

C examples for Pointer:Function Pointer

Introduction

You can get the address of a function by using the function's name without any parentheses or arguments.

Demo Code

#include <stdio.h>
#include <string.h>

void check(char *a, char *b, int(*cmp)(const char *, const char *));
int main(void) {
   char s1[80], s2[80];
   int(*p)(const char *, const char *); /* function pointer */

   p = strcmp; /* assign address of strcmp to p */

   printf("Enter two strings.\n");
   gets_s(s1);//from   ww w .  j  a  va2  s .c  om
   gets_s(s2);

   check(s1, s2, p); /* pass address of strcmp via p */

   return 0;
}

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

Result


Related Tutorials