Abu Bakr Mohamed Ramadan eng.abubakr@gmail.com
Content:  What is function pointer?  Motivation, what is the use cases of function pointer?  Declaration of function pointer in c  Initialization of function pointer in c  Calling a function using the function pointer  Example on function pointer  Function pointer as arguments
What is function pointer?  A function pointer is one of the most important pointer tools which is often ignored and misunderstood by the people.  A function pointer is similar to the other pointers but the only difference is that it points to a function instead of the variable.  Unlike normal pointers, a function pointer points to code, not data. Typically a function pointer stores the start of executable code.
Motivation, what is the use cases of function pointer?  Interrupt Vector table is an array of pointers to function (ISR),  Callback mechanism in modular layered application,  Basic concept in OS and RTOS programing,  we can achieve OOP concepts like inheritance and polymorphism in C using function pointers.
Declaration of function pointer in c  int fpData(int);  int *fpData(int);  int (*fpData)(int);  int (*fpData)(char *);  int* (*fpData)(char *);  int (*fpData)(int, char *);
Initialization of function pointer in c  declaration of function pointer int (* pfAddTwoNumber) (int, int);  Initialize the function pointer with the address of AddTwoNumber pfAddTwoNumber = & AddTwoNumber; or pfAddTwoNumber = AddTwoNumber;  We can also initialize the function pointer in a single line. int (* pfAddTwoNumber) (int, int) = AddTwoNumber;
Calling a function using the function pointer  First, write the name of a function pointer. pfAddTwoNumber;  We know that function pointer is also similar to another pointer but the only difference is that function pointer pointed to a function except for the variable. So we put the * as a prefix to the name of function pointer to take the value of the function pointer. *pfAddTwoNumber;  Finally, we have a function. For the function calling just simply add the arguments list with extra parenthesis to the above expression. (*pfAddTwoNumber)(10, 20);
Example #include <stdio.h> #include <stdlib.h> //function used to add two numbers int AddTwoNumber(int iData1 ,int iData2) { return (iData1 + iData2); } int main(int argc, char *argv[]) { int iRetValue = 0; //Declaration of function pointer int (*pfAddTwoNumber)(int,int) = NULL; //initialise the function pointer pfAddTwoNumber = AddTwoNumber; //Calling the function using function pointer iRetValue = (*pfAddTwoNumber)(10,20); //display addition of two number printf("nnAddition of two number = %dnn",iRetValue); return 0; }
Function pointer as arguments  we can pass the function pointer as an argument into the function like the other pointers.  Example on Function pointer as arguments

Pointer to function 1

  • 1.
    Abu Bakr MohamedRamadan eng.abubakr@gmail.com
  • 2.
    Content:  What isfunction pointer?  Motivation, what is the use cases of function pointer?  Declaration of function pointer in c  Initialization of function pointer in c  Calling a function using the function pointer  Example on function pointer  Function pointer as arguments
  • 3.
    What is functionpointer?  A function pointer is one of the most important pointer tools which is often ignored and misunderstood by the people.  A function pointer is similar to the other pointers but the only difference is that it points to a function instead of the variable.  Unlike normal pointers, a function pointer points to code, not data. Typically a function pointer stores the start of executable code.
  • 4.
    Motivation, what isthe use cases of function pointer?  Interrupt Vector table is an array of pointers to function (ISR),  Callback mechanism in modular layered application,  Basic concept in OS and RTOS programing,  we can achieve OOP concepts like inheritance and polymorphism in C using function pointers.
  • 5.
    Declaration of functionpointer in c  int fpData(int);  int *fpData(int);  int (*fpData)(int);  int (*fpData)(char *);  int* (*fpData)(char *);  int (*fpData)(int, char *);
  • 6.
    Initialization of functionpointer in c  declaration of function pointer int (* pfAddTwoNumber) (int, int);  Initialize the function pointer with the address of AddTwoNumber pfAddTwoNumber = & AddTwoNumber; or pfAddTwoNumber = AddTwoNumber;  We can also initialize the function pointer in a single line. int (* pfAddTwoNumber) (int, int) = AddTwoNumber;
  • 7.
    Calling a functionusing the function pointer  First, write the name of a function pointer. pfAddTwoNumber;  We know that function pointer is also similar to another pointer but the only difference is that function pointer pointed to a function except for the variable. So we put the * as a prefix to the name of function pointer to take the value of the function pointer. *pfAddTwoNumber;  Finally, we have a function. For the function calling just simply add the arguments list with extra parenthesis to the above expression. (*pfAddTwoNumber)(10, 20);
  • 8.
    Example #include <stdio.h> #include <stdlib.h> //functionused to add two numbers int AddTwoNumber(int iData1 ,int iData2) { return (iData1 + iData2); } int main(int argc, char *argv[]) { int iRetValue = 0; //Declaration of function pointer int (*pfAddTwoNumber)(int,int) = NULL; //initialise the function pointer pfAddTwoNumber = AddTwoNumber; //Calling the function using function pointer iRetValue = (*pfAddTwoNumber)(10,20); //display addition of two number printf("nnAddition of two number = %dnn",iRetValue); return 0; }
  • 9.
    Function pointer asarguments  we can pass the function pointer as an argument into the function like the other pointers.  Example on Function pointer as arguments

Editor's Notes

  • #6 . It seems like difficult in beginning but once you are familiar with function pointer then it becomes easy. void ( *pfDisplayMessage) (const char *); In above expression, pfDisplayMessage is a pointer to a function taking one argument, const char *, and returns void.