Function Pointer Ex Code
Function Pointer Ex Code
h>
// Function prototypes
int add(int a, int b);
int subtract(int a, int b);
int multiply(int a, int b);
int divide(int a, int b);
Next, define a function that takes a function pointer as an argument along with two integers.
This function will call the pointed-to function with the provided integers.
c
Copy code
// Function pointer type definition
typedef int (*operation_t)(int, int);
Finally, use the function pointers in the main function to call the different arithmetic
operations.
c
Copy code
int main() {
int a = 10, b = 5;
// Perform addition
perform_operation(add, a, b);
// Perform subtraction
perform_operation(subtract, a, b);
// Perform multiplication
perform_operation(multiply, a, b);
// Perform division
perform_operation(divide, a, b);
return 0;
}
c
Copy code
#include <stdio.h>
// Function prototypes
int add(int a, int b);
int subtract(int a, int b);
int multiply(int a, int b);
int divide(int a, int b);
// Function definitions
int add(int a, int b) {
return a + b;
}
int main() {
int a = 10, b = 5;
// Perform addition
perform_operation(add, a, b);
// Perform subtraction
perform_operation(subtract, a, b);
// Perform multiplication
perform_operation(multiply, a, b);
// Perform division
perform_operation(divide, a, b);
return 0;
}