Function Pointers: Ananda Gunawardena
Function Pointers: Ananda Gunawardena
Function Pointers
In this lecture
Functions with variable number of arguments
Introduction to function pointers
Example of passing a function pointer to a function (qsort)
Defining a function pointer
Generic Data Types
printf(char*, );
where first argument char* is a string that contains the format statement for the print
string (eg: %d %x %i ) and the 3 dots () indicate a variable number of arguments
equal to number of format symbols in the print string.
Let us consider writing our own function sum that can take a variable number of
arguments. That is, we can call the function as sum(2,x,y) or sum(3,x,y,z) etc.
The <stdarg.h> is a macro library that contains a set of macros, which allows portable
functions, that, accepts variable argument lists to be coded. Functions that have variable
argument lists (such as printf()) do not use these macros are inherently non-portable, as
different systems use different argument-passing conventions. There are few things in the
above code that we need to understand.
va_arg() This macro returns the next argument in the list pointed to by ap. Each
invocation of va_arg() modifies ap so that the values of successive arguments are
returned in turn. The type parameter is the type the argument is expected to be. This is the
type name specified such that the type of a pointer to an object that has the specified type
can be obtained simply by suffixing a * to type. Different types can be mixed, but it is up
to the routine to know what type of arguments are expected.
va_end() This macro is used to clean up; it invalidates ap for use (unless va_start() is
invoked again).
Function Pointers are pointers, i.e. variables, which point to an address of a function. The
running programs get a certain space in the main memory. Both, the executable compiled
program code and the used variables, are put inside this memory. Thus a function in the
program code is also an address. It is only important how the compiler and processor
interpret the memory the function pointer points to. A function can take many types of
arguments including the address of another function. This can be an extremely elegant
way to bind a function to a specific algorithm at runtime. For example, let us assume that
we plan to use a sorting algorithm in one of our functions. At the compile time, we are
not sure which sorting algorithm to use since we may not know the size of the data set n.
For example, if n < 100 we may use something like insertion sort, or if n is large, then
we may decide to use something like quicksort. C provides an interesting way to achieve
this by allowing the programmer to decide the algorithm at runtime.
% man qsort
NAME
qsort - sorts an array
SYNOPSIS
#include <stdlib.h>
DESCRIPTION
The qsort() function sorts an array with nmemb elements of size size.
The base argument points to the start of the array.
The comparison function must return an integer less than, equal to, or
greater than zero if the first argument is considered to be respec-
tively less than, equal to, or greater than the second. If two members
compare as equal, their order in the sorted array is undefined.
RETURN VALUE
The qsort() function returns no value.
int A[1000],i;
for (i=0;i<1000;i++)
A[i] = rand()%1000;
qsort(A,1000,sizeof(int),intcompare);
char B[][10]={"guna","mccain","obama","paul","barr"};
qsort(*B,5,10,namecmp);
for (i=0;i<5;i++)
printf("%s \n",B[i]);
int (*fn)(int,int) ;
Here we define a function pointer fn, that can be initialized to any function that takes
two integer arguments and return an integer. Here is an example of such a function
int main(){
printf(%d \n, mystery(10,12,sum));
printf(%d \n, mystery(10,12,gcd));
printf(%d \n, mystery(10,12,sumofsqures));
}
Using Typedefs
The syntax of function pointers can sometimes be confusing. So we can use a typedef
statement to make things simpler.
typedef <return_type> (* fpointer)(argument list);
Another Example
This can also be very useful in writing functions that return function pointers such as
follows.
The above function takes a char as an argument and returns a function pointer of type Ptr
(as defined above) based on if the char is + or
Function pointer is the address where the function begins in the memory. Function name
can be used to refer to this address just as we use the name of an array to refer to the
address of the first element of the array. Although function name can be preceded by &,
we can ignore the & when referring to a function pointer. As an example, consider the
function average defined as
Now we can define a function pointer variable fn to hold the address of average as:
fn = average;
Now fn is an alias for average and we can compute the value of the function at 10.5 and
20.5 as, for example using the code,
In fact any function, that takes two doubles as arguments and return a double can be
assigned this function pointer fn. Many programmers try to avoid function pointers
because of the complex syntax. However, to make things easy, we can define a function
pointer type alias using
This makes it easy to deal with the confusing function pointer notation.
takes a function pointer of type ftype as an argument. Similarly, we can return a function
pointer from a function as follows. The function foo
returns a function pointer of type ftype from the function foo. Next we will look at an
important application of the function pointers. We do know that Java 5 and C++ Standard
Template Library (STL) contains constructs to define generic functions. Generic
functions are great programmer tool, as the programmer needs to write the code once for
any data type and bind the type at the run time. Unfortunately C does not have any built
in APIs or libraries to handle algorithms that take generic data types. But we can easily
define generic functions using C. Generic functions are quite useful, as they can be
adapted to data of many types. In other words, we can implement an algorithm in
the most general way so that any data type can be used with the function.
Generic Functions
One of the major benefits of a function pointer is that it can be passed to another function
as one of the arguments. This can be quite useful when we try to write generic functions.
Suppose we want to write a function to find how many elements are in an array (not the
capacity, but how many places are taken). The Array can be an array of ints or array of
characters or array of any other type. All we need to know is how to find the end of the
array. For example, if we have an array of ints that has a sentinel (say ends with -999 or
something like that), we can write a function that returns true when array element is
equal to -999. Otherwise it returns false. Consider the following function.
The above function returns true if A[i] = -999 for some i. So how do we use such a
function?
Suppose we build a generic function arraylen as follows. The purpose of the function is to
find the size of the array (not the capacity) regardless of the data type. For example, an
array of ints may have -999 as a sentinel elements, an array of characters may have \0 as
the sentinel, or an array of strings may have something like end as the final string. So
regardless of the sentinel we need to write a function that can find the array length (not
Convince yourself that this function returns the length of the array when called with
arraylen(A, (fn)intfunc)
Exercise: Write a function stringfunc as in intfunc that returns true if S[i] =\0
We can give more examples of how to use generic functions for doing lots of interesting
things. Generally, if we write a sort function, using bubble sort algorithm for an array of
ints, it would look something like:
The problem with the above function is that it works only for an array of ints. Now let us
suppose we need to write a sort function using bubble sort algorithm for a generic array.
We now not only pass the array and its size, we also pass another function cmp, that will
define how to compare two values from the array. We may also need to pass a swap
function that defines how to swap two elements in the array. This is important since
comparison and swap can be very specific to a data type. For example, while we can
compare ints or doubles using < or >, we need to use strcmp to compare strings.
First let us develop a compare functions that returns 1, 0 or -1 based on if the first
argument is greater, the same or smaller than the second argument. As an example, we
will develop a compare function that compares strings using just its first character.
The above function can be passed to any other function that can use the algorithm of how
to compare two strings using just the first characters of the string.
Now let us suppose that we have an array of ANY TYPE and we need to sort it using
bubble sort algorithm. We will need to send four arguments to this function.
We note that this is needed since the way we compare and swap integers may be different
from way we compare and swap strings. No matter what data type we have in the array,
we need to define a generic bubble sort algorithm.
First we will define two type aliases cmpfunc and swapfunc. The cmpfunc, takes any
array and compares two elements and return 1, 0 or -1. The swapfunc, takes an array and
two indices, swap the content of the array. Given below are the two typedefs that can be
used.
We note that the function takes any array, its size, and a compare function and a swap
function that can help compare and swap array elements.
The above functions clearly define how to compare and swap two elements in an integer
array. How do we use these functions in our bubble sort?
We can define an array of ints and simply call the bubble sort as follows.
This technique can be used to write any function for an array of any type. We only need
to write the algorithm once and send the proper functions based on the type of the array.