Function Pointers

Function Pointers: A function pointer is a variable which is used to hold the starting address of a functions and the same can be used to invoke a function. It is also possible to pass address of different functions at different times thus making the function more flexible and abstract. 

So the function pointers can be used to simplify code by providing a simple way to select a function to execute based on run-time values. Function pointers do always point to a function having a specific signature. Thus, all functions used with the same function pointer must have the same parameters and return type.


Uses of Function Pointers:
  • Function pointers are the only way for "Interrupt programming". In UNIX all the Interrupts are called using function pointers. This is mainly used in system programming. 
          If a process wishes to handle certain signals then in the code, the process has to register 
          a signal handling function to the kernel. 
          The following is the prototype of a signal handling function :
           void <signal handler funcname> (int sig)

          The signal handler function has void return type and accepts a signal number            
           corresponding to the signal that needs to be handled. 

          To get the signal handler function registered to the kernel, the signal handler function            
           pointer is passed as second argument to the ‘signal’ function. 
          The prototype of the 
            signal function is : void (*signal(int signo, void (*func )(int)))(int);

  • One of the major application of the function pointer is call back function. 
          A callback is done just like a normal function call would be done: You just use the name of 
          the function pointer instead of a function name. 
          void qsort( ... , int(_USERENTRY *cmpFunc)(const void*, const void*))
          {
                  /* sort algorithm - note: item1 and item2 are void-pointers */
                          int bigger=cmpFunc(item1, item2); // make callback
                 /* use the result */
          }

  • Function pointer is also used in function pointer table. 
          #include <stdio.h>
          #include <stdlib.h>
          void f1(void);
          void f2(void);
          void f3(void);
          void f4(void);
          int main(void)
          {
                    void (*table[])(void)={&f1,&f2,&f3,&f4}; /* table of function pointers */
                    void (**p)(void); /* pointer to a function pointer */
                    p=table;
                    while(1)
                   {
                               (*(*p))(); /* Call function @p */
                               ++p;
                    };
          }
         void f1(void){puts("F1 CALLED");return;};
         void f2(void){puts("F2 CALLED");return;};
         void f3(void){puts("F3 CALLED");return;};
         void f4(void){puts("F4 CALLED"); exit(0);};

  • A function pointer is used to pass a function as an argument to another function, or to store a function as a data item, for example a list of functions can be implemented as an array of pointers to functions 
         Passing Function Pointer as an argument
               #include <stdio.h>
               #include<stdlib.h> 
               void joy(int (*debug) () ) 
               { 
                     Debug(“joy\n”); 
               } 
               Int main() 
               { 
                     joy(printf); 
               } 
         
         Calling the function using Function Pointer
               #include<stdio.h> 
               int func (int a, int b)                
               { 
                      printf("\n a = %d\n",a); 
                      printf("\n b = %d\n",b); 
                     return 0; 
                } 
               int main(void) 
                { 
                       int(*fptr)(int,int); // Function pointer 
                       fptr = func; // Assign address to function pointer 
                       func(2,3); 
                       fptr(2,3); 
                     return 0; 
                 } 
          Output: a = 2;b = 3; 
                       a = 2;b = 3;

No comments:

Post a Comment