Created
May 25, 2020 07:27
-
-
Save YuehChuan/2b7eebc5e59c3ba1090194acea6e867b to your computer and use it in GitHub Desktop.
functionPtr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
//function pointer | |
typedef int (*MathMethod)(int, int); | |
int Mul(int a, int b) { return a*b; } | |
float Divide(int a, int b) { return a/b; } | |
int Minus(int a, int b) { return a-b; } | |
int Add(int a, int b) { return a+b; } | |
int *Calc(int x, int y, MathMethod Opt){ | |
return Opt(x, y); | |
} | |
int main() | |
{ | |
int a = 6, b = 4; | |
printf("a x b = %d\n", Calc(a, b, Mul)); | |
printf("a / b = %d\n", Calc(a, b, Divide)); | |
printf("a + b = %d\n", Calc(a, b, Minus)); | |
printf("a - b = %d\n", Calc(a, b, Add)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment