Passing Pointer to Function in C 

Passing Pointer to Function in C Language with Examples

In this article, I will discuss Passing a Pointer to a Function in C Language with Examples. Please read our previous articles discussing Pointer to Array of functions in C Language with Examples.

Pointer to Function in C Language

Passing a pointer to a function in C is a powerful technique often used to create flexible and reusable code. This approach is especially popular in scenarios like implementing callbacks, where you want to pass a function as an argument to another function, allowing the called function to “call back” the passed-in function.

When you pass a pointer to a function, you pass the function’s address. The called function can then use this pointer to execute the function that was passed to it. Suppose you have a simple function like this:
void greet() {
       printf(“Hello, World!\n”);
}

You can define a pointer to this function and pass it to another function:
void (*ptr)() = greet;

Example: Simple Callback Function Using Pointer to a Function in C

This example demonstrates passing a function pointer to another function, allowing the called function to execute the callback.

#include <stdio.h>

// Function prototype
void executeCallback(void (*callbackFunc)());

// Callback function
void myCallback() {
    printf("Callback function called.\n");
}

int main() {
    executeCallback(myCallback);
    return 0;
}

// Function accepting a function pointer
void executeCallback(void (*callbackFunc)()) {
    printf("Inside executeCallback function.\n");
    callbackFunc();  // Call the callback function
}

In this example, executeCallback takes a function pointer as an argument and calls the function pointed to by that pointer.

Example: Passing a Function Pointer with Parameters in C

Let’s modify the example to use functions with parameters.

#include <stdio.h>

// Function prototypes
void executeCallback(void (*callbackFunc)(int));
void myCallback(int num);

int main() {
    executeCallback(myCallback);
    return 0;
}

void executeCallback(void (*callbackFunc)(int)) {
    printf("Inside executeCallback function.\n");
    callbackFunc(5);  // Call the callback function with an argument
}

void myCallback(int num) {
    printf("Callback function called with num = %d\n", num);
}

In this case, myCallback takes an int as a parameter, and executeCallback calls myCallback with a specific integer.

Example: Using Function Pointers for a Strategy Pattern in C Language

Function pointers can be used to implement the strategy pattern, allowing the selection of an algorithm at runtime.

#include <stdio.h>

// Function prototypes
int add(int a, int b);
int multiply(int a, int b);
void compute(int (*operation)(int, int), int a, int b);

int main() {
    compute(add, 5, 3);
    compute(multiply, 5, 3);
    return 0;
}

int add(int a, int b) {
    return a + b;
}

int multiply(int a, int b) {
    return a * b;
}

void compute(int (*operation)(int, int), int a, int b) {
    int result = operation(a, b);
    printf("Result: %d\n", result);
}

In this example, compute takes a function pointer operation and two integers. It computes and prints different results depending on the function passed (add or multiply).

Key Points:
  • The syntax for function pointers can be confusing at first. Remember that the function pointer’s type must match the signature of the functions it’s intended to point to.
  • Passing function pointers increases the flexibility of your code but can also add complexity. It’s important to use them in contexts where such dynamism is needed.
  • This feature is especially useful in event-driven programming, implementing callbacks, or choosing between multiple functions at runtime.

In the next article, I will discuss Character Pointers in C Language. In this article, I explain Passing a Pointer to a Function in C Language with Examples. I hope you enjoy this article. I would like to have your feedback. Please post your feedback, questions, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *