Function Pointers in C with Example Programs

function-pointers-in-c-language-with-example-programs

Introduction:

In previous articles, we looked at pointers in C and Double pointers in C with example programs. In this article, we will look at function pointers in c programming language with examples.

📢 Must Check: Pointers Practice Programs series

Function Pointers in C Language:

The function pointers are pointer variables, which are capable to store the memory address of a function.

Function pointers are used to create and use the callbacks and we can also pass a function as an argument to another function using the function pointers in C.

📢 Function pointers store the memory address of a function.

The Memory Address of a Function in C:

As we have discussed earlier in the introduction to pointers variables, Variables are stored in the memory and each memory cell has a memory address. Similarly, Every function will have a memory address.

We can get the memory address of a function by simply specifying the function name (without a function call ( ) ). As the function name also specifies the address of the function.

Let’s look at a program to get the address of a function.

Program to Get the address of a function:

In the following program, we have created a function addition and printed its address on the console.

Program Output:

Compile and Run the program.

function-address-in-programming

We have defined a function called addition to add two numbers num1 and num2. Then we printed the address( 0x5653f1791169) of the addition function on the console using the function name and address of operator(‘&’).

Now we know how to access the function addresses, let’s look at how to declare a function pointer and assign the function address to the function pointer.

How to Declare Function Pointers in C Language:

We can create a function pointer by function prototype that pointer going to store. Here is the syntax of the function pointer.

Syntax of Function Pointer in C:

Example of function pointer:

Here we have declared a function pointer named func_ptr. The func_ptr points to a function with two integer arguments and returns an integer value.

let’s look at another example.

The random_ptr is a function pointer, that can store the address of a function that returns a character( char) and accepts a character pointer( char *) as an argument.

Assigning functions to a function pointer:

Now that we know how to declare a function pointer, Let’s look at how we can assign a function to a function pointer.

Let’s say we have a function called product which takes two integers as input and returns an integer variable.

Declare a function pointer called product_ptr to hold the address of the product function.

As discussed earlier, The function name also specifies the address of the function, We can simply assign the product function product_ptr function like below.

Now, The product_ptr function pointer is now storing the address of the product function.

Calling a function using the Function Pointer in C Programming:

We can call the function product using the function pointer product_ptr by dereferencing the function pointer like below.

Syntax to call the function using a function pointer:

Note that we passed the two integer numbers number1 and number2 as the arguments to the function pointer and The product function returns an integer variable and we stored the return value in the result variable.

Let’s combine all the above operations and create a program to declare, assign and use the function pointers in C.

Program to understand the function pointer in C programming language:

The following program demonstrates how to declare a function pointer, assign a function to a function pointer, and finally call a function using a function pointer in c programming language.

Program Output:

Let’s compile the program using your favorite compiler. We are using the GCC compiler in this example.

$ gcc function-pointer.c

Test Case 1:

The above command generates an executable file called a.out. Run the executable file using the following command.

As we can see from the above output, The function pointer product_ptr is properly storing the function address and we are able to get the product by dereferencing the function pointer.

Let’s look at another example.

Test Case 2:

function-pointers-in-c-example-program-output

The program is properly calculating the product of the given two integers.

Function pointer vs Function declaration in C:

If you observe the above function pointer declaration, It is very similar to the function forward declaration.

📢 Please learn more about function forward declaration in the following article.

For example, If we have a function called addition with the following function definition

We can forward declare the above function using the following function declaration.

Let’s create a function pointer to store the address of the addition function. We can create a function pointer like below.

As we are creating the pointer variable, we need to specify the asterisk symbol( *) before the pointer name.

The parentheses are also needed otherwise, it will become the function declaration.

Let’s look at why we need parentheses. If you forgot to specify the parentheses, We end up creating a function declaration instead of the function pointer.

In the above example, We haven’t specified the parentheses while creating the function pointer, So it became a function declaration of a function returning an integer pointer ( int *).

📢 The precedence of the parenthesis ( ) is higher than the asterisk *, Please look at the Operators Precedence and Associativity Table to understand the operator precedencies in the c programming language.

An Array of Function pointers in C :

An array of function pointers is an array and each element of the array is a function pointer. When you have a collection of functions that all carry out the same operations and you want to select one of them at runtime based on a criterion, an Array of function pointers can be used.

We can declare an array of function pointers by specifying the size of the array like below.

Here we have defined an array of function pointers func_ptr_arr with the size of 5 elements. Each element of the array is a function pointer, that returns an integer value and takes two integers as arguments.

We can assign the functions to the array of function pointers like below.

Here the function1, function2, function3, function4, and function5 are functions that take two integer values and return an integer value.

Finally, We can call the functions using the array of function pointers by dereferencing the function pointer like below.

Program to understand the Array of Function Pointers in C Language:

Let’s look at a program to uses the array of function pointers in c programming language.

Program Output:

Let’s compile and run the program.

array-of-function-pointers-in-c-programming-language

As we can see from the above output, The program performed all arithmetic operations using the array of function pointers.

Function Pointers uses in C Programming:

In C, function pointers are used to create and use callbacks, and we can also use function pointers to pass a function as an argument to another function.

One example of function pointer usage in the c language is with the signal() function in Linux OS.

Here the sighandler_t is a function pointer.

We also use the function pointer in qsort() function to sort arrays, and strings.

The compare function is a function pointer.

Conclusion:

We have looked at the function pointers in C programming language in detail. We covered how to declare a function pointer, how to assign a function to a function pointer, and finally how to call a function through the function pointer to get the desired results.

Related Articles:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] Function Pointers in C with Example Programs […]

Leave a Reply