Pointing to Functions
We have already discussed how pointers to array work.
Similarly, you can declare a pointer that is initialized with the left value of
function (i.e. with the address at which function is stored). You can then call
the function via a pointer. This concept is useful in passing functions to
other functions.
When a
function declaration appears in another function, the name of the function
being declared becomes a pointer to that function. Such pointers can ba passed
to functions as arguments. The first function can be accessed within the second
function as though it was a variable.
The declaration of function which accepts another function
as argument is of the form:
return_type (*func_name) (parameter_list);
Here * is added before function name so that it must be
identified as a pointer to another function. The parentheses around it indicate
that it is a pointer to a function and not a function that returns a pointer.
The return type indicates the data type that function returns. The parameter
list represents the list of parameters in the function.
The function call is of the form,
(*func_name) (argument_list);
The indirection operator (*) must precced the function name
and both must be enclosed in parentheses so as to maintain the priority. The
argument list is the list of arguments if any.
Post a Comment