C allows operations with pointers to functions. We have seen earlier in this chapter that every function code along with its variables is allocated some space in the memory. Thus, every function has an address.
FUNCTION
POINTERS
C
allows operations with pointers to functions. We have seen earlier in this
chapter that every function code along with its variables is allocated some
space in the memory. Thus, every function has an address. Function pointers are
pointer variables that point to the address of a function. Like other pointer
variables, function pointers can be declared, assigned values, and used to
access the functions they point to.
This
is a useful technique for passing a function as an argument to another
function. In order to declare a pointer to a function we have to declare it
like the prototype of the function except that the name of the function is
enclosed between parentheses () and an asterisk (*) is inserted before the
name. The syntax of declaring a function pointer can be given as
return_type
(*function_pointer_name) (argument_list);
Look
at the declaration below in which we declare a pointer to a function that
returns an integer value and accepts two arguments one of type int and the other of type float.
int (*func) (int a, float b);
Because
of precedence, if you do not put the function name within parenthesis, you will
end up declaring a function returning a pointer as shown:
/* function returning pointer to
int */ int *func (int a, float b);
As
in case of other pointer variables, a function pointer must be initialized
prior to use. If we have declared a pointer to the function, then that pointer
can be assigned the address of the correct function just by using its name.
Like in the case of an array, a function name is changed into an address when
it's used in an expression. It is optional to use the address operator (&)
in front of the function name.
For
example, if fp is a function pointer and we have a function add () with
prototype given as
int add (int, int);
Then
writing fp = add; initializes the
function pointer fp with the address of add
().
When
a pointer to a function is declared, it can be called using one of two forms:
(*func) (1,2);
OR
func (1,2);
Look
at the program given below which makes use of a pointer to a function.
#include <stdio.h>
void print(int n);
void (*fp) (int);
main()
{
fp = print;
(*fp) (10);
fp (20);
return 0;
}
void print (int value)
{
printf("\n %d", value);
}
Output
10
20
Now
let us write another code that illustrates how the contents of fp can be
changed at run time to point to two different functions during program
execution.
#include <stdio.h>
float (*func) (float, float);
//Define a function pointer
float add (float, float);
float sub (float, float);
main ()
{
func = add; // function pointer
points to add
printf("\n Addition %f",
func (9.5, 3.1));
func = sub; // function pointer
points to sub
printf("\n Subtraction =
%f", func (9.5, 3.1));
}
float add (float x, float y)
{
return (x + y);
}
float sub (float x, float y)
{
return (x - y);
}
Output
Addition = 12.80000
Subtraction = 8.40000
A
function pointer can be declared and initialize to NULL as shown below:
int (*fp) (int) = NULL;
Comparison
operators such as = = and != can be used the same way as usual. Consider the
code given below which checks if fp
actually contains the address of the function print (int).
if (fp >0) { // check if
initialized
if (fp == print)
printf("\n Pointer points to
print ");
else
printf("\n Pointer not
initialized!");
}
A
function pointer can be passed as the calling argument of a function. This is
in fact necessary if you want to pass a pointer to a callback function. The
following code shows how to pass a pointer to a function which returns an int and accepts two int values.
Note
that in the program below, the function operate calls the functions add and subtract
with the following line:
result = (*operate_fp) (num1,num2);
#include <stdio.h>
int add (int, int);
int subt (int, int);
int operate (int (*operate_fp)
(int, int), int, int);
main()
{
int result;
result = operate (add, 9, 7);
printf ("\n Addition result =
%d", result); n
result = operate (sub, 9, 7);
printf ("\n Subtraction =
%d", result);
return 0;
}
int add (int a, int b)
{
return (a + b);
}
int subtract (int a, int b)
{
return (a - b);
}
int operate (int (*operate_fp)
(int, int), int a, int b)
{
int result;
result = (*operate_fp) (a, b);
return result;
}
Output
Addition = 16
Subtraction = 2
Programming in C: Unit III (b): Pointers : Tag: : with Example C Programs - Function Pointers
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation
Professional English II
HS3251 2nd Semester 2021 Regulation | 2nd Semester Common to all Dept 2021 Regulation
Statistics and Numerical Methods
MA3251 2nd Semester 2021 Regulation M2 Engineering Mathematics 2 | 2nd Semester Common to all Dept 2021 Regulation
Engineering Graphics
GE3251 eg 2nd semester | 2021 Regulation | 2nd Semester Common to all Dept 2021 Regulation
Physics for Electrical Engineering
PH3202 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation
Basic Civil and Mechanical Engineering
BE3255 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation
Electric Circuit Analysis
EE3251 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation
Physics for Electronics Engineering
PH3254 - Physics II - 2nd Semester - ECE Department - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation
Electrical and Instrumentation Engineering
BE3254 - 2nd Semester - ECE Dept - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation
Circuit Analysis
EC3251 - 2nd Semester - ECE Dept - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation
Materials Science
PH3251 2nd semester Mechanical Dept | 2021 Regulation | 2nd Semester Mechanical Dept 2021 Regulation
Basic Electrical and Electronics Engineering
BE3251 2nd semester Mechanical Dept | 2021 Regulation | 2nd Semester Mechanical Dept 2021 Regulation
Physics for Civil Engineering
PH3201 2021 Regulation | 2nd Semester Civil Dept 2021 Regulation
Basic Electrical, Electronics and Instrumentation Engineering
BE3252 2021 Regulation | 2nd Semester Civil Dept 2021 Regulation
Physics for Information Science
PH3256 2nd Semester CSE Dept | 2021 Regulation | 2nd Semester CSE Dept 2021 Regulation
Basic Electrical and Electronics Engineering
BE3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation