Linux C Programming Tutorial Part 25 - Function pointers

Up until now in this ongoing C programming tutorial series, we have discussed the basic concept of pointers as well as quite a few aspects related to pointers, like pointer to an array and array of pointers. Expanding upon our understanding of pointers, in this tutorial, we will discuss the concept of pointers to functions.

Function pointers in C programming language

Just like we have pointers to variables, there can also be pointers to functions. Following is an example of a function pointer declaration:

void (*fn_ptr)(int)

So here we have a function pointer named 'fn_ptr' that can point to any function which returns void and accepts an integer as input. Needless to say, this is just the declaration part - like any other pointer, you need to assign it an address (that of a function in this case) to make use of it.

Following is an example making use of this pointer:

#include <stdio.h>

void print_int(int a)
{
printf("\n The integer value is: %d\n",a);
}

int main()
{
void (*fn_ptr)(int);
fn_ptr = &print_int;
(*fn_ptr)(10);

return 0;
}

So as you can see, we first defined a function 'print_int' that accepts an integer and returns void. Then, in the 'main' function, we declared 'fn_ptr' as a function pointer that can point to functions like 'print_int'. This was followed by assigning address of 'print_int' function to 'fn_ptr', and finally, making a call to the function using the pointer.

Here's the output produced:

The integer value is: 10 

What's worth mentioning here is that you can further simplify this program by avoiding & and * from last two lines. Following is the modified code:

#include <stdio.h>

void print_int(int a)
{
printf("\n The integer value is: %d\n",a);
}

int main()
{
void (*fn_ptr)(int);
fn_ptr = print_int;
fn_ptr(10);

return 0;

Moving on, like an array of pointers, you can also have an array of function pointers. For example, following is an array of function pointers capable of storing 3 function addresses.

void (*fn_ptr[3])(int)

And following is an example making use of this array of pointers:

void print_int1(int a)
{
printf("\n The integer value is: %d\n",a);
}

void print_int2(int a)
{
printf("\n The integer value is: %d\n",a+1);
}

void print_int3(int a)
{
printf("\n The integer value is: %d\n",a+2);
}

int main()
{
void (*fn_ptr[3])(int);

fn_ptr[0] = print_int1;
fn_ptr[1] = print_int2;
fn_ptr[2] = print_int3;

fn_ptr[0](10);
fn_ptr[1](10);
fn_ptr[2](10);

return 0;
}

Here's the output produced by this code:

The integer value is: 10 

The integer value is: 11

The integer value is: 12

Another aspect of function pointers you should know about is that you can use them as function arguments as well. For example, there can be a function that accepts a pointer to a function as an argument. For example:

void some_random_func(void (*fn_ptr)(int)) 

Following is an example code that utilizes this function:

#include <stdio.h>

void another_random_func(int a)
{
printf("\n The integer to entered is: %d\n", a);
}

void some_random_func(void (*fn_ptr)(int))
{
fn_ptr(5);
}


int main()
{
some_random_func(another_random_func);
return 0;
}

So what we did here is, we created a function dubbed 'some_random_func' which accepts a function pointer in the input. Then, from 'main', we called 'some_random_func' with the address of another function 'another_random_func' as an argument. Then using the pointer, we successfully called 'another_random_func'.

Here's the output:

The integer to entered is: 5 

Conclusion

Function pointers can come in handy when you want to create something called a 'callback mechanism' (read more about it here). But before getting into that, it's better if you get a good understanding of this concept. We suggest you try examples from this tutorial on your local machine (and also create new ones). In case of any doubt or query, leave a comment below.

Share this page:

2 Comment(s)