Last active
April 2, 2016 17:45
-
-
Save devsli/824480473466a3177d09 to your computer and use it in GitHub Desktop.
Simple function ptr
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
// $ a.out 1 "+" 2 | |
// 3 | |
// $ a.out 2 "*" 3 | |
// 6 | |
// $ a.out 6 "/" 2 | |
// 3 | |
#include <assert.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
typedef long (*op)(int a, int b); | |
long add(int a, int b) { return a + b; } | |
long sub(int a, int b) { return a - b; } | |
long dvv(int a, int b) { return a / b; } | |
long mul(int a, int b) { return a * b; } | |
long eq (int a, int b) { return a == b; } | |
int main(int argc, char **argv) { | |
op o; | |
assert(strlen(argv[2]) == 1); | |
switch (argv[2][0]) { | |
case '+': o = &add; break; | |
case '-': o = ⊂ break; | |
case ':': o = &dvv; break; | |
case '*': o = &mul; break; | |
case '=': o = &eq ; break; | |
default: break; | |
} | |
printf("%ld\n", o(atoi(argv[1]), atoi(argv[3]))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment