Function Pointer in C With Examples

A function pointer in C is a pointer that points to a function.

The C language has given a way for us to making certain statements execute faster, by allowing us to reference executable code within a function.

This reference involves the function pointer, which points to this code in memory (RAM).

Since we’re calling this function through a variable, such a call is called an indirect call to this function.

Let’s understand this, by illustrating how this reference is made to a simple swap() function.


C Swap Function Reference Asm

If we were to make a function pointer that references the swap() function, it will point to the executable code, which is shown in the assembly language.

This will make the overall execution speed much faster, since we directly refer to a lower level code.

Now that we have our concepts covered, let’s look at how we can actually write a function pointer for our swap function.

Defining a Function Pointer

Before defining the function pointer, let’s break down what any function consists of.

Any function will have a return type, arguments passed to it, and a name, such as:

The function pointer simply points to such a function, so we can define a function pointer like this:

This says that function_ptr is a function pointer which points to a function that takes 2 char arguments and returns an int.

Notice the enclosing parenthesis (*function_ptr). This is because we must explicitly specify that it is a pointer. Otherwise, the compiler may think that we are pointing to a function that returns int* and throw an error.

Similarly, for a function that is defined like this:

The function pointer is defined in this way:

Now that we’ve defined our function pointers, let’s now make a reference to the functions, by pointing to the function name.

Note that you don’t need to explicitly mention the referencing ampersand (&), since functions are implicitly passed as pointers anyway, so the following code is still valid.

Direct Initialization

Alternatively, we can directly point to the function during declaration itself, like any other pointer.

Now that we’ve looked at initializing the pointer, let’s now see it in action!

Using Function Pointers

This is really simple! We just need to de-reference our function pointer by passing appropriate arguments, and it will run the executable code which it is pointing to. That’s it!

Again, you don’t need to explicitly mention the de-referencing asterisk (*), since functions are implicitly passed as pointers anyway. But if you do, you must explicitly mention the parenthesis (*function_ptr)(arg1, arg2), since a function call has a higher precedence over a de-reference.

If the function takes no arguments, you can pass void to the parameter list.

Let’s do this for our swap() function.

Another Example

To complete this article, we’ll show you a complete example.

This is a simple calculator program that utilizes function pointers to handle cases easily. Elegant, isn’t it?


Another way to make a function pointer point to multiple other function codes is with the use of a function pointer array. Have a look at the sample code below.


Hopefully, you’ve learned how you can use a function pointer in C. If you have any queries, do ask them on the comment section below!


By admin

Leave a Reply

%d bloggers like this: