Advertisement
heinrich23

funcptrtest.c

Nov 15th, 2022
3,780
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.88 KB | None | 0 0
  1. include <stdio.h>
  2.  
  3. const int b = 23;
  4.  
  5. // A normal function with an int parameter and void return type
  6. void fun(int a)
  7. {
  8.   if (a < b)
  9.     printf("Value of a (%d) is lesser then value of b (%d)\n", a, b);
  10.   else if (a == b)
  11.     printf("Value of a (%d) is equal value of b (%d)\n", a, b);
  12.   else if (a > b)
  13.     printf("Value of a (%d) is greater than value of b (%d)\n", a, b);
  14. }
  15.  
  16. // main
  17. int main(int argc, char **argv)
  18. {
  19.     int i = 0;
  20.     /* fun_ptr is a pointer to function fun()
  21.            void (*fun_ptr)(int) = &fun;
  22.          is equivalent of following two
  23.            void (*fun_ptr)(int);
  24.            fun_ptr = &fun;
  25.     */
  26.     void (*fun_ptr)(int) = &fun;
  27.  
  28.     // call once with static int
  29.     (*fun_ptr)(b);
  30.  
  31.     // iterate through for loop
  32.     for (i = 1; i < 101; i+=11)  {
  33.         // Invoking fun() using fun_ptr
  34.         (*fun_ptr)(i);
  35.     }
  36.  
  37.     return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement