In C, pointers can not only point to data variables but also to functions. A pointer to a function allows you to call a function indirectly through the pointer, enabling dynamic function calls and the implementation of callback mechanisms. This concept is useful in scenarios like event handling and passing functions as arguments to other functions.

Program: Pointer to Function

Here's a simple C program that demonstrates the use of pointers to functions:

#include <stdio.h> // Function prototypes void add(int a, int b); void subtract(int a, int b); void multiply(int a, int b); void divide(int a, int b); int main() { // Declare a function pointer void (*operation)(int, int); int num1, num2; char op; // User input for two numbers and an operator printf("Enter two numbers: "); scanf("%d %d", &num1, &num2); printf("Enter operation (+, -, *, /): "); scanf(" %c", &op); // Space before %c to ignore newline character // Assign the appropriate function to the function pointer based on the operator switch (op) { case '+': operation = add; break; case '-': operation = subtract; break; case '*': operation = multiply; break; case '/': operation = divide; break; default: printf("Invalid operation!\n"); return 1; // Exit the program with an error code } // Call the function using the function pointer operation(num1, num2); return 0; } // Function implementations void add(int a, int b) { printf("Result: %d\n", a + b); } void subtract(int a, int b) { printf("Result: %d\n", a - b); } void multiply(int a, int b) { printf("Result: %d\n", a * b); } void divide(int a, int b) { if (b != 0) { printf("Result: %.2f\n", (float)a / b); } else { printf("Error: Division by zero!\n"); } }

Explanation of the Program

  1. Header Files:

    • #include <stdio.h>: This header file is included for input/output functions like printf and scanf.
  2. Function Prototypes:

    • The function prototypes declare the functions add, subtract, multiply, and divide that will be defined later.
  3. Function Pointer Declaration:

    • void (*operation)(int, int);: This line declares a pointer named operation that can point to any function taking two int parameters and returning void.
  4. User Input:

    • The program prompts the user to enter two numbers and an operation (+, -, *, or /).
  5. Function Pointer Assignment:

    • A switch statement checks the operator entered by the user and assigns the appropriate function to the operation pointer.
  6. Calling the Function:

    • The function is called indirectly using the pointer: operation(num1, num2);. This line calls the function that operation points to, passing the two numbers as arguments.
  7. Function Implementations:

    • Each function (add, subtract, multiply, and divide) performs the respective operation and prints the result. The divide function also checks for division by zero to prevent runtime errors.

How to Run the Program

  1. Compile the Code: Use a C compiler like gcc to compile the code:

    gcc pointer_to_function.c -o pointer_to_function
  2. Execute the Program:

    ./pointer_to_function

Example Output

When you run the program, the output might look like this:

Enter two numbers: 10 5 Enter operation (+, -, *, /): + Result: 15

Or for another operation:

Enter two numbers: 10 5 Enter operation (+, -, *, /): / Result: 2.00

Conclusion

This program demonstrates how to use pointers to functions in C, enabling dynamic function calls based on user input. Understanding function pointers allows for more flexible and reusable code, as functions can be passed around like variables. This concept is particularly useful in implementing callback functions, handling events, and designing more modular code.