Last active
February 9, 2019 22:57
-
-
Save waltervargas/7355413f87c5540bdde03403fa682e63 to your computer and use it in GitHub Desktop.
C function pointers
This file contains hidden or 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> | |
| #define safeFree(p) saferFree((void**)&(p)) | |
| void saferFree(void **pp) { | |
| if (pp != NULL && *pp != NULL) { | |
| free(*pp); | |
| *pp = NULL; | |
| } | |
| } | |
| typedef int (*fptrOperation)(int,int); | |
| typedef int (*operation)(int,int); | |
| operation operations[128] = {NULL}; | |
| int add(int num1, int num2){ | |
| return num1 + num2; | |
| } | |
| int sub(int num1, int num2){ | |
| return num1 - num2; | |
| } | |
| int multi(int num1, int num2){ | |
| return num1*num2; | |
| } | |
| int square(int num1){ | |
| return num1*num1; | |
| } | |
| void initializeOperationsArray() { | |
| operations['+'] = add; | |
| operations['-'] = sub; | |
| operations['*'] = multi; | |
| operations['^'] = (fptrOperation)square; | |
| } | |
| int evaluateArray(char opcode, int num1, int num2){ | |
| fptrOperation op; | |
| op = operations[(int)opcode]; | |
| return op(num1, num2); | |
| } | |
| int main(int argc, char **argv) { | |
| // while(argc--) | |
| // printf("%s\n", *argv++); | |
| // the first 128 ASCII characters | |
| initializeOperationsArray(); | |
| printf("%d\n", evaluateArray('+', 5, 6)); | |
| printf("%d\n", evaluateArray('-', 5, 6)); | |
| printf("%d\n", evaluateArray('*', 5, 6)); | |
| printf("%d\n", evaluateArray('^', 5, 0)); | |
| return (EXIT_SUCCESS); | |
| } |
This file contains hidden or 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> | |
| typedef int (*fptrOperation)(int,int); | |
| typedef int (*operation)(int,int); | |
| operation operations[128] = {NULL}; | |
| int add(int num1, int num2){ | |
| return num1 + num2; | |
| } | |
| int sub(int num1, int num2){ | |
| return num1 - num2; | |
| } | |
| int multi(int num1, int num2){ | |
| return num1*num2; | |
| } | |
| int square(int num1){ | |
| return num1*num1; | |
| } | |
| void initializeOperationsArray() { | |
| operations['+'] = add; | |
| operations['-'] = sub; | |
| operations['*'] = multi; | |
| operations['^'] = (fptrOperation)square; | |
| } | |
| int evaluateArray(char opcode, int num1, int num2){ | |
| fptrOperation op; | |
| op = operations[(int)opcode]; | |
| return op(num1, num2); | |
| } | |
| int main(int argc, char **argv) { | |
| // the first 128 ASCII characters | |
| initializeOperationsArray(); | |
| printf("%d\n", evaluateArray('+', 5, 6)); | |
| printf("%d\n", evaluateArray('-', 5, 6)); | |
| printf("%d\n", evaluateArray('*', 5, 6)); | |
| printf("%d\n", evaluateArray('^', 5, 0)); | |
| return (EXIT_SUCCESS); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment