Function Pointers
Contents
Typedef
The "typedef" keyword can be used to assign another name for a type declaration. This is often used in "C++" to make declarations easier to use.File: type1.cpp
#include<stdio.h> int main() { int x1 ; int* ptr1 ; x1 = 100 ; printf("x1 value is %d\n" , x1 ); ptr1 = &x1 ; printf("%d \n" , *ptr1 ); return 0 ; } Output: 100 100In the above example we are using an integer type and a pointer to an integer type. We can rewrite the above program as:
File: type2.cpp
#include<stdio.h> int main() { typedef int INTEGER ; typedef int* POINTER_TO_INTEGER ; INTEGER x1 ; POINTER_TO_INTEGER ptr1 ; x1 = 100 ; printf("x1 value is %d\n" , x1 ); ptr1 = &x1 ; printf("%d \n" , *ptr1 ); return 0 ; }The above example shows the declaration of "typedef" and it's usage. It's as if we had new types.
Function Pointers
We have seen examples of a pointer pointing to an integer but we can also have a pointer pointing to a function.File: func1.cpp
#include<stdio.h> void function1( int param1 ) { printf( "Am inside function1: %d \n" , param1) ; } void function2( int param1 ) { printf( "Am inside function2: %d \n" , param1) ; } int main() { void (*POINTER_FUNCTION)(int a) ; POINTER_FUNCTION = function1 ; POINTER_FUNCTION(100) ; POINTER_FUNCTION = function2 ; POINTER_FUNCTION(200) ; return 0 ; } $ g++ func1.cpp ; ./a.exe Am inside function1: 100 Am inside function2: 200
The declaration: void (*POINTER_FUNCTION)(int a) ; states that the "POINTER_FUNCTION" is a pointer to a function. However it is not just any function but a function with a particular signature. The function must take an int as an argument and return void. The "POINTER_FUNCTION" is actually a variable and we can assign the address using the form: POINTER_FUNCTION = function1 ; and then invoke the pointer using : POINTER_FUNCTION(100) ; This ends up calling the function with an argument value of 100 . We can change the value that the pointer holds and assign the value of function 2 to it .
File: func2.cpp
#include<stdio.h> void function1( int param1 ) { printf( "Am inside function1: %d \n" , param1) ; } void function2( int param1 ) { printf( "Am inside function2: %d \n" , param1) ; } int main() { void (*POINTER_FUNCTION)(int a) ; POINTER_FUNCTION = function1 ; (*POINTER_FUNCTION)(100) ; POINTER_FUNCTION = function2 ; (*POINTER_FUNCTION)(200) ; return 0 ; } $ g++ func2.cpp ; ./a.exe Am inside function1: 100 Am inside function2: 200The above shows the syntax "(*POINTER_FUNCTION)(100)" that can be used to call the function also.
File: func3.cpp
#include<stdio.h> void function1( int param1 ) { printf( "Am inside function1: %d \n" , param1) ; } void function2( int param1 ) { printf( "Am inside function2: %d \n" , param1) ; } int main() { typedef void (*POINTER_FUNCTION_TYPE)(int a) ; POINTER_FUNCTION_TYPE ptr = function1 ; ptr(100) ; ptr = function2 ; ptr(200 ) ; return ( 0 ) ; }The notation
void (*POINTER_FUNCTION)(int a) ;
is kind of confusing . We are used to saying "int x1" where we have a type and then a variable of that type. We can use "typedef" to rewrite out progra
Using "typedef" above we define a type "POINTER_FUNCTION_TYPE" and then we can use this type to create a variable "ptr". This variable can hold the address of a function and is a function pointer.