Like pointer to data(int, char, struct etc) we can have pointer to functions as well.
The convention to use function pointer is same as calling the name of the function.
Example:
// function to be called via function pointer. Note the return type and arguments
void testFunction(int data)
{
// Do something on data
printf("%d is the number passed via function pointer\n", data) ;
return ;
}
// Main function
int main()
{
// funPtr is function pointer, returning void and accepting an int
// void (*funPtr)(int) ;
// Now point funptr to point the right function
void (*funPtr)(int) = &testFunction; // &testFunction is address of fun stored, try printing *testFunction or *funPtr
*funPtr(10) ; // called testFunction
return 0 ;
}
Above program will call testFunction() via function pointer.
Points to remember
1) function pointers are not allocated by any memory like data poiners.
2) function pointers will be used for call back functions.
3) You can call function pointer without using *.
4) Function pointers will be mainly used for runtime which function to use.
5) quick sort and binary search standard library functions uses function pointers.
6) Like normal variables passed to function as arguments, the same way function pointers will also used for the same. Check quick sort and other sorting methods to sort the numbers in ascending and descending order.
7) virtual functions are nothing but implementation of function pointers.
8) Most OS code has function pointers, lots of functionality for file system, memory management, scheduling etc has function pointers involved.
9) Events are triggered via function pointers.
The convention to use function pointer is same as calling the name of the function.
Example:
// function to be called via function pointer. Note the return type and arguments
void testFunction(int data)
{
// Do something on data
printf("%d is the number passed via function pointer\n", data) ;
return ;
}
// Main function
int main()
{
// funPtr is function pointer, returning void and accepting an int
// void (*funPtr)(int) ;
// Now point funptr to point the right function
void (*funPtr)(int) = &testFunction; // &testFunction is address of fun stored, try printing *testFunction or *funPtr
*funPtr(10) ; // called testFunction
return 0 ;
}
Above program will call testFunction() via function pointer.
Points to remember
1) function pointers are not allocated by any memory like data poiners.
2) function pointers will be used for call back functions.
3) You can call function pointer without using *.
4) Function pointers will be mainly used for runtime which function to use.
5) quick sort and binary search standard library functions uses function pointers.
6) Like normal variables passed to function as arguments, the same way function pointers will also used for the same. Check quick sort and other sorting methods to sort the numbers in ascending and descending order.
7) virtual functions are nothing but implementation of function pointers.
8) Most OS code has function pointers, lots of functionality for file system, memory management, scheduling etc has function pointers involved.
9) Events are triggered via function pointers.
No comments:
Post a Comment