Skip to main content

C-language

Function Pointers - Everything you need to know!

Function pointers are pointers that point to code. When these are dereferenced, the fetched data is treated like instructions and the CPU executes them.

💡
When we dereference a function pointer, the PC register in the CPU is set to the address held by the pointer.

Consider typedef void (*function_ptr_t)(void); to be the function pointer type. Because of the typedef keyword, the function_ptr_t is treated like a new type. As per this definition, the pointer is to return nothing and takes no parameters. Try the following code -

#include <stdio.h>

typedef void (*function_ptr_t)(void);

void random_function(void) {
    printf("Hello, World!\n");
}

int main() {
    function_ptr_t fptr = random_function;
    fptr();
    return 0;
}

main.c!

The output should be - Hello, World!. What happened here was, the fptr variable was of the type function_ptr_t which is a function pointer type. The fptr was pointing to the address where the code of random_function was saved. When we dereference the pointer with the syntax - fptr() the address is loaded into the program counter and the CPU fetches the instructions to execute.

Hopefully, you are convinced how a function pointer works. Now let's look at some more details and use cases.


C Pointers: Secrets every Embedded Engineer must know!

This course will enable you to think and use pointers like a professional senior/staff engineer in ~6Hrs - Ways to 

  1. Visualize
  2. Think,
  3. Reason, and
  4. Design

Using pointers as experts do. The course takes a detailed hands-on approachexplaining the code, the theory, and the underlying system details one should consider when dealing with pointers in C. It is full of graphics, annotation and hands on demo that the students can try along in GitHub Codespaces.

More Details: https://inpyjama.com/c-pointers-course/

Access The C Pointers Course

The Syntax

The general syntax is return type (* function pointer name)(parameter list). The function/code being pointed to should have the same return type and parameters. For example, the snippets below add has the same return types and parameter list as fn.

The (* and ) around a function name makes it a function pointer :)

Without typedef

Using typedef is not strictly required, if we decide not to use it, the function pointer variable declaration will be as in the code below. Notice how the syntax is long and hard to read.

#include <stdio.h>

int add(int x, int y) {
    return x + y;
}

int main() {
    int (*fn)(int a, int b) = add;
    printf("1 + 2 = %d\n", fn(1, 3));
    return 0;
}

without typedef

With typedef

typedef will create a new type for us which happens to be called fn. We can use fn just like any other data type, as used to define fpt. This as you see, is more readable.

#include <stdio.h>

typedef int (*fn)(int a, int b);

int add(int x, int y) {
    return x + y;
}

int main() {
    fn fpt = add;
    printf("1 + 2 = %d\n", fpt(1, 3));
    return 0;
}

with typedef we create a new type called fn and use that to create the pointer variable fpt.

When to use function pointers?