What is Function Pointers?
Function pointer
is same as pointer to variables except that function pointers will points to the
address of function to be called. Every function and its associated variables
will be stored in some memory location. When
we assign function address to function pointer it will store the starting
address of function. Function pointers
can be used in task schedulers and state machines. Function pointers provide efficient and
easily manageable programming techniques.
We can implement
efficient task schedulers to call a function from task table. We will discuss state machine implementation
using function pointer in next blogs. Main steps to use function pointers is definition of function
pointer, assigning address to function pointer and calling a function using
function pointer.
Function pointer declaration
Function pointer declaration mainly
contains three parameters:
- Return time
- Function pointer name
- Parameter list
Any function pointer name should be preceded by “*” and enclosed in parentheses.
Return_type
(*functionpointer_name) (parameter list)
Return type can be void, int, char,
float etc, function pointer_name will be the name of the function pointer and
parameter list will be list of parameters function passes.
Example of function pointer:
void (*funptr1)(void); //function
pointer declaration for the //function that takes no thing and return //nothing.
int (*funptr2)(int, float); //
this function pointer takes two //values integer and floats and //return integer value
int (*funptr2)( int*); // this function pointer takes pointers
to a // variables and return integer value
We can also pass the one function
pointer to another function pointer.
int (*funptr2)(int, char, void (*funptr1)(void));
//this function pointer accept the variables and a function pointe //and return integer value.
Assigning Function address to Function Pointers
Assigning function address to function pointers is same as
assigning variable address to pointer variable.
Function_pointer_name
= &Function_name;
“&”
operator will give the address of function pointer. Example of function pointers
are as follows:
funptr1=&Fuction1;
funptr2=&Function2;
Before using function pointer it should keep mind that
function pointer should be initialized. If function pointer is not initialized or
contain a garbage value then program start execution from address pointed by function
pointer. If address is out of range then whole system can be crash. You can
check function pointer to be initialized by comparing it with null variable or
address of function. If function pointer contains null value it means it does
not set. So don’t call function pointer.
if(funptr1!=NULL)
{
//call
function
}
else
{
//do
no thing
}
Calling a function using function pointer
It requires
three parameter to call a function using pointers, result_value if function
pointer is returning something, function pointer name preceded by “*” mark and
parameter list.
Result_value (*function_pointer_name)(parameter list);
If function
does not returning anything we can omit the result i.e.:
(*function_pointer_name)(Parameter list);
For
examples:
(*funptr1); //call the void Function1 pointed by function
pointer //funptr1
value = (*funptr2)(4, 1.5);// call void Function2 pointed by //function
pointer funptr2 and return the //integer value in value variable.
Continued..............
No comments:
Post a Comment