C Program to Demonstrate Function Pointers

C Program to Demonstrate Function Pointers

Function pointers are pointers that store the address of a function instead of a variable. They allow you to call functions indirectly, pass functions as arguments, and implement dynamic behavior such as callback mechanisms.

In C programming, understanding function pointers is crucial for writing flexible and modular code. Function pointers are often used in event-driven programming, dynamic dispatch, and scenarios where you need to select a function at runtime.

In this tutorial, we will explore the basics of function pointers, demonstrate how to declare and use them, and provide practical examples with detailed explanations.

Understanding the Problem

The goal is to use pointers to store and call functions. Unlike normal pointers that point to variables, function pointers point to executable code. Using a function pointer, you can invoke different functions dynamically without hardcoding which function to call.

We will start with simple examples and gradually show how function pointers can be passed as arguments to other functions and even returned from functions.

Program 1: Basic Function Pointer

This example demonstrates how to declare a function pointer, assign a function to it, and call the function.

#include <stdio.h>

void greet() {
    printf("Hello, welcome to function pointers!\n");
}

int main() {

    void (*funcPtr)(); // Declare a function pointer
    funcPtr = greet;   // Assign the address of the function

    // Call the function using the pointer
    funcPtr();

    return 0;

}

In this code, funcPtr is a pointer to a function that takes no arguments and returns void. Assigning greet to funcPtr allows us to call the function indirectly using funcPtr().

Program 2: Function Pointers with Arguments

Function pointers can also point to functions that accept arguments and return values.

#include <stdio.h>

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

int main() {

    int (*operation)(int, int); // Function pointer with two int parameters
    operation = add;

    int result = operation(5, 10); // Call function using pointer
    printf("Sum = %d\n", result);

    return 0;

}

Here, operation is a pointer to a function that takes two integers and returns an integer. Using operation(5, 10) calls add() indirectly, showing how function pointers work with arguments and return values.

Program 3: Passing Function Pointers as Arguments

Function pointers can be passed to other functions to implement callbacks or dynamic behavior.

#include <stdio.h>

void sayHello() {
    printf("Hello!\n");
}

void sayGoodbye() {
    printf("Goodbye!\n");
}

void execute(void (*func)()) {
    func(); // Call the passed function
}

int main() {

    execute(sayHello);   // Pass sayHello function
    execute(sayGoodbye); // Pass sayGoodbye function

    return 0;

}

In this example, execute() accepts a function pointer as a parameter. We can pass different functions to execute(), allowing dynamic selection of which function to run at runtime.

Program 4: Returning Function Pointers

Functions in C can return pointers to other functions, allowing even more dynamic behavior.

#include <stdio.h>

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

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

int (*getOperation(char op))(int, int) {

    if (op == '+') return add;
    else return multiply;

}

int main() {

    int (*operation)(int, int);

    operation = getOperation('+');
    printf("Result of addition: %d\n", operation(5, 3));

    operation = getOperation('*');
    printf("Result of multiplication: %d\n", operation(5, 3));

    return 0;

}

Here, getOperation() returns a pointer to a function based on the character op. If op is '+', it returns the address of add, otherwise it returns multiply. This allows dynamic selection of the operation at runtime.

FAQs

1. What is a function pointer?
A function pointer is a pointer that stores the address of a function and can be used to call the function indirectly.

2. Can function pointers accept arguments?
Yes, function pointers can point to functions with parameters, and you can call them with the required arguments.

3. Why use function pointers?
They allow dynamic selection of functions at runtime, implement callbacks, and help write modular, reusable code.

4. Can a function return a function pointer?
Yes, functions can return pointers to other functions, enabling dynamic selection of functionality during runtime.

5. Are function pointers safe?
They are safe when used with matching signatures and valid functions. Incorrect use can lead to undefined behavior.

Conclusion

Function pointers are a powerful feature in C that allow you to store addresses of functions, call them dynamically, pass them as arguments, and even return them from other functions.

We explored basic function pointers, pointers with arguments, passing function pointers as parameters, and returning function pointers. Mastering function pointers enables flexible code design, supports callback mechanisms, and prepares you for advanced programming patterns like dynamic dispatch and event-driven programming.

References & Additional Resources

A curated collection of textbooks, tutorials, and documentation for learning function pointers and general pointer usage in C.

  1. Kernighan, B.W., & Ritchie, D.M. (1988). The C Programming Language (2nd Edition). Prentice Hall – The authoritative text covering pointers, arrays, functions, and core C programming concepts.
  2. GeeksforGeeks: Function Pointers in C – Explains how to declare, assign, and use function pointers in C with examples.
  3. Tutorialspoint: C Function Pointers – Beginner-friendly guide to function pointers, including syntax and common use cases.
  4. cplusplus.com: Pointer Basics – Reference covering pointer concepts, arithmetic, and memory addressing, including function pointers.
Scroll to Top