C Programming

Function Pointers in C with Examples

A function pointer is mutable that holds the location of a method that may be invoked later using that address. Since methods contain behavior, this seems to be helpful. Instead of creating a chunk of code each moment, we require a specific action, like drawing lines; you just have to invoke the method. However, with basically a similar code, we could want to adopt various actions at different moments. For specific instances, continue following this guide to the end.

Syntax:

The syntax for defining a function pointer may appear complicated initially, though it is actually pretty simple if you grasp what’s happening on. Consider the following syntax:

void (*foo)(int);

Foo is a reference to a function that takes one parameter, an integer, as well as yields void throughout this instance. It was as if you declared “*foo”, a method that accepts an int & returns void; since *foo is a method, then foo has to be a reference to a method. Likewise, int *x could be interpreted as *x is an int, implying that x is a reference to an int. The best way of making a method pointer declaration would be to write out a method statement although with (*func_name) instead of func_name.

To see the working of function pointers, let open the Ubuntu 20.04 Linux system first. After that, try to open the terminal shell in your system utilizing the Ctrl+Alt+T. After opening the terminal, you have to make sure that your system has a C compiler installed and configured because we have been working on the C programming language. If not installed, make sure to update your apt package first and then install the GCC compiler using the apt command as follows.

$ sudo apt update

$ sudo apt install gcc

Example 01:

After the terminal has been ready for a while, make a new C language file having a C extension with any name. In Linux, we use the “touch” query to create such sort of files. Hence use the below query to create a file “main.c” in your home directory of the Ubuntu 20.04 system:

$ touch main.c

Now the file has been created. We have first to open it to add C code to it. To open the file, you can use any editor that has been configured on your system. We preferred the GNU nano editor to open the file and edit. Hence, we have used the “nano” keyword to open the file “main.c” in the GNU editor as follows:

$ nano main.c

You will get a purple window screen at your terminal shell. Now write out the below code in it. This code is simply telling how to do the initialization of function pointers in C language. We have included the standard package library for input and output. We have declared a function “func” with an integer type parameter. This method contains a print statement to execute the variable “z”. The main method has been used to start the execution of code. This method contains a function pointer in it. One should provide a method pointer to the location of a method in our code to start it. The syntax is the same as it is for any other variable. The trick is to analyze the phrase from the inside out, observe that the inner component is *foo and that the rest of the phrase seems like a regular method declaration. *foo must be used to refer to a method taking an int & yields a void. As a result, foo is a reference to a method “func” of this kind. As we haven’t passed any value to the “func” method, that’s why there will be empty output.

The compilation has been done using the gcc compiler. After that, the execution of this C file has been done using the a.out command. As there were no values passed in the function parameter, hence empty output has been yielded.

$ gcc main.c

$ ./a.out

Example 02:

This time, we will be using the very same example from the above code. But, this time, we will only change things, passing value to the function. Therefore, open the file once again as:

$ nano main.c

We have used two function calls here. One of them is a simple function call passing “4” into its parameter. The second function invoking related to a pointer with the value “4” has been passed in its parameter. To invoke the method referred to with a function pointer, consider it as if this was the method’s name to be called. The process of invoking it performs the dereference; there is no requirement to fix it yourself.

The same queries have always done compilation and running of the file. We have the output of our updated code. It displays 4 as the integer value to simple function “func” and a pointer function in the output. This is how the function pointer works.

$ gcc main.c

$ /a.out

Example 03:

Let’s have another simple example for the function pointer. To update the existing file, open it via nano editor as below:

$ nano main.c

The code has been updated as presented in the picture. We have added a new function, “add,” having two integer type parameters and returning the sum of both integer numbers. The compilation will be started from the main method. The main method contains function pointer foo. This method “add” has been relating to pointer “add’. We have called the pointer function first, then the original function “add’ with some values passed to both statements. These results of the sum will be saved to integer variables “c1” and “c2”. Then both values in these variables will be printed out in the shell via printf statement.

Compilation and execution of code have outputted the string in print statements and the values that are being calculated in the function “add” as a sum.

Example 04:

Let’s have our last example. Open the main.c file again to update it.

$ nano main.c

Write out the below script of C in it. This time we have used pointer type variables in the parameter of function “func”. Two integer-type variables have been created and saved values of pointer variables in both. The nested if-else statement has been initialized if the variable 1 is less than variables 2, or both are equal, or there is some other case. Whatever the situation is same value will be returned to the main method. In the main array, “A” with size 8 has been declared, and a loop has been started to add values to array A while decrementing 1 from it. Then these elements will are sorted with method “qsort,” and then the array will be displayed.

Compile:

Executed shows he sorted array.

Conclusion:

We have done some simple examples to see the methodology of function pointers. Hope you got it easy to implement and learn using this tutorial.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.