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:

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:

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:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image1-30.png" data-lazy- height="19" src="data:image/svg xml,” width=”338″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image3-28.png" data-lazy- height="273" src="data:image/svg xml,” width=”506″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image2-28.png" data-lazy- height="38" src="data:image/svg xml,” width=”319″>

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:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image5-22.png" data-lazy- height="21" src="data:image/svg xml,” width=”335″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image4-25.png" data-lazy- height="288" src="data:image/svg xml,” width=”474″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image3-29.png" data-lazy- height="273" src="data:image/svg xml,” width=”506″>

Example 03:

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image6-19.png" data-lazy- height="18" src="data:image/svg xml,” width=”323″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image9-12.png" data-lazy- height="324" src="data:image/svg xml,” width=”496″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image8-13.png" data-lazy- height="75" src="data:image/svg xml,” width=”315″>

Example 04:

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image12-11.png" data-lazy- height="18" src="data:image/svg xml,” width=”328″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image10-12.png" data-lazy- height="404" src="data:image/svg xml,” width=”481″>

Compile:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image11-12.png" data-lazy- height="16" src="data:image/svg xml,” width=”323″>

Executed shows he sorted array.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image13-9.png" data-lazy- height="179" src="data:image/svg xml,” width=”337″>

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

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Author-Image-150×150.jpg60f2fc9a575ed.jpg" height="112" src="data:image/svg xml,” width=”112″>

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.