Function pointers in C allow a function to be called indirectly through a pointer variable rather than directly by name. A function pointer declaration specifies the return type and parameters like a normal function but with an asterisk prefixing the name. Function pointers can be assigned the address of an existing function. To call a function through a pointer, the pointer is dereferenced using asterisk or arrow notation followed by parentheses and arguments. Functions can have pointer parameters to pass references rather than values, allowing changes to persist outside the function. Function pointers provide flexibility in deciding which function to call at runtime.
2. INTRODUCTION
• Function pointers are pointers that point to functions instead of data types.
• They can be used to allow variability in the function that is to be called, at
run-time.
Syntax:
•returnType (*name)(parameters)
•typedef returntype (*name)(parameters)
•typedef returnType Name(parameters);
Name *name;
•typedef returnType Name(parameters);
typedef Name *NamePtr;
3. DECLARING A FUNCTION POINTER IN C
Now that we know that functions have a unique memory address, we can
use Function pointers in C that can point to the first executable code
inside a function body.
SYNTAX OF FUNCTION POINTER IN C
return_type (* pointer_name) (datatype_arg_1, datatype_arg_1, ...);
4. • Declaring a function pointer in C is comparable to declaring a function
except that when a function pointer is declared, we prefix its name which is
an Asterisk * symbol.
• For example, if a function has the declaration
float foo (int, int);
• Declaration Of a function pointer in C for the function foo will be
// function pointer declaration
float (*foo_pointer) (int, int);
/* assigning the address of the function (foo) to function pointer */
foo_pointer = foo;
5. CALLING A FUNCTION THROUGH A FUNCTION
POINTER IN C
• Calling a function using a pointer is similar to calling a function in the
usual way using the name of the function.
• Suppose we declare a Function and its pointer as given below
int (*pointer) (int); // function pointer declaration
int areaSquare (int); // function declaration
pointer = areaSquare;
6. To call the function areaSquare, we can create a function call using any of
the three ways
int length = 5;
// Different ways to call the function
// 1. using function name
int area = areaSquare(length);
// 2. using function pointer (a)
int area = (*pointer)(length);
// 3. using function pointer (b)
int area = pointer(length);
•The effect of calling functions using pointers or
using their name is the same.
7. FUNCTIONS USING POINTER VARIABLES
• C allows pointers to be passed in as function arguments and also return
pointers from the function.
• To pass pointers in the function, we simply declare the function parameter
as pointer type.
• When functions have their pointer type arguments, the changes made on
them inside the function persists even after program exists function scope
because the changes are made on the actual address pointed by the pointer.
• This approach to pass arguments to a function is called as pass by
reference because as shown in the figure below reference of the variable is
passed to the function instead of the value stored in the address.
8. SAFE WAYS TO RETURN A POINTER FROM A
FUNCTION
1. Return variables are either created using the keyword static or
created dynamically at run time because such variables exist in
memory beyond the scope of the called function.
2. Use arguments that are passed by their reference because such
functions exist in the calling function scope.
9. FUNCTIONS USING POINTER VARIABLES
• C allows pointers to be passed in as function arguments and also return
pointers from the function.
• To pass pointers in the function, we simply declare the function parameter
as pointer type.
• When functions have their pointer type arguments, the changes made on
them inside the function persists even after program exists function scope
because the changes are made on the actual address pointed by the pointer.
• This approach to pass arguments to a function is called as pass by
reference because as shown in the figure below reference of the variable is
passed to the function instead of the value stored in the address.
10. REFERENCING AND DEREFERENCING OF
FUNCTION POINTER IN C
• Suppose we want to create a sorting function. It makes more sense to allow
the function’s caller to decide the order in which values are sorted (ascending,
descending, etc).
• One way is to provide a flag in the function argument to decide what to do,
but this is not flexible.
• Another way is to provide user flexibility to pass a function in our sort
function. This function can take two values as input and perform a
comparison between them. A syntax for our new function will look like.
void sort(int array[], int n, function comparison);