0
0
CHow-ToBeginner · 3 min read

How to Create Callback Using Function Pointer in C

In C, you create a callback by defining a function pointer that points to a function matching a specific signature, then pass it as an argument to another function. The receiving function calls the pointer to execute the callback function. This allows flexible code where behavior can be customized at runtime.
📐

Syntax

A callback in C uses a function pointer which stores the address of a function. The syntax to declare a function pointer is:

  • return_type (*pointer_name)(parameter_types);
  • Example: void (*callback)(int); means a pointer to a function taking an int and returning void.

You can pass this pointer as a parameter to another function and call it inside that function using pointer_name(arguments);.

c
void callback_function(int x);

void caller_function(void (*callback)(int)) {
    callback(5);  // Call the callback function
}
💻

Example

This example shows how to define a callback function, pass it to another function, and call it inside that function.

c
#include <stdio.h>

// Callback function matching this signature
void my_callback(int num) {
    printf("Callback called with value: %d\n", num);
}

// Function that accepts a callback function pointer
void perform_action(void (*callback)(int)) {
    printf("Inside perform_action function.\n");
    callback(10);  // Call the callback
}

int main() {
    // Pass the callback function to perform_action
    perform_action(my_callback);
    return 0;
}
Output
Inside perform_action function. Callback called with value: 10
⚠️

Common Pitfalls

Common mistakes when using function pointers for callbacks include:

  • Not matching the function pointer signature exactly with the callback function.
  • Forgetting to use parentheses when calling the function pointer.
  • Passing NULL pointers without checking before calling.

Always ensure the callback function signature matches the pointer type and check pointers before calling.

c
/* Wrong: signature mismatch */
void wrong_callback(float x) { }

void caller(void (*callback)(int)) {
    callback(5);  // Error if callback signature mismatches
}

/* Correct: matching signature */
void correct_callback(int x) { }

void caller_correct(void (*callback)(int)) {
    callback(5);  // Safe call
}
📊

Quick Reference

ConceptExampleDescription
Function pointer declarationvoid (*cb)(int);Pointer to function taking int, returning void
Passing callbackvoid func(void (*cb)(int));Function accepts callback pointer as parameter
Calling callbackcb(5);Invoke the callback function via pointer
Matching signaturevoid cb(int x);Callback function must match pointer signature

Key Takeaways

Define a function pointer with the exact signature of the callback function.
Pass the function pointer as a parameter to another function to create a callback.
Call the callback inside the receiving function using the function pointer syntax.
Always ensure the callback function signature matches the pointer type to avoid errors.
Check for NULL pointers before calling a callback to prevent crashes.