C Language Neat Tricks #2 (Part II)

You could call it a neat trick or just something so oddball insane that few programmers would dare breech the territory: A variable that holds the address of a function; a function pointer.

Pointers are scary enough in C, but a function pointer variable?

In last week’s Lesson, I showed how one function works as an argument to some other function. That operation takes place because a function has an address, a location in memory. That’s a pointer!

It’s possible in C to create a function pointer — a variable that holds the address of a function. This possibility begs the question: Why?

Because this post, like the last two, is about oddball things in C that most programmers don’t think of. This is definitely an item some interloping Python programmer would consider as insane. And that’s fine by me.

The format to declare a function variable looks like this:

ret_type (*name)(arg_type);

ret_type is the function’s return value, such as int, void, or whatever.

name is the function’s name as a variable. It’s a pointer, so the asterisk is prefixed to the name. Because of the order of precedence, the name and asterisk must be enclosed in parentheses.

arg_type is the list of function arguments, just as you’d find in the function’s prototype. I recommend specifying only the types (int, void, and so on), not specific variable names.

As with all pointer variables, you must initialize the function pointer to a function’s address to use it. After that, you can use the function pointer’s name to reference the function.

Here’s your sample code:

#include <stdio.h>

int funct1(void)
{
    return(42);
}

int funct2(void)
{
    return(24);
}

int main()
{
    int (*fp)(void);        /* variable fp is a function pointer */

    fp = funct1;
    printf("The solution is %d\n",fp());
    fp = funct2;
    printf("The solution is %d\n",fp());

    return(0);
}

The output:

The solution is 42
The solution is 24

The function pointer fp is declared in Line 15. It’s assigned the address of funct1() at Line 17, then called in Line 18. It’s assigned to funct2() at Line 19, and used again at Line 20.

Now to seriously address the interloping Python programmer’s inquiry: Why bother?

Honestly, there is absolutely no reason to use a function pointer as shown in the sample code.

The only time you need to use a function pointer construction is when your code contains a function that requires another function as an argument — as is done in the qsort() function. In that case, the following format is implemented in the function’s declaration as well as inside the function. For example:

void qsort(void *base, size_t nel, size_t width, int (*compar)(const void *a, const void *b));

Inside the function, the variable compar(a,b) is used to call the user-define function that compares the two values for the sort, which is just how the variable fp() is used in this Lesson’s sample code.

That’s it!

Any other insanity you want to try using the function pointer technique is purely up to you.

Leave a Reply