Created
July 7, 2014 14:43
-
-
Save afaquejam/15a46f86cba2dddc3434 to your computer and use it in GitHub Desktop.
C Function Pointers Example
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> | |
int add(int, int); | |
int substract(int, int); | |
int perform(int (*doMath)(int, int), int a, int b) { | |
return doMath(a, b); | |
} | |
void main (int argc, char *argv[]) { | |
int (*doMath)(int, int); | |
int var = 1; | |
if(var) | |
printf("%d \n", perform(add, 1, 2)); | |
else | |
printf("%d \n", perform(substract, 1, 2)); | |
} | |
int add(int a, int b) { | |
return a + b; | |
} | |
int substract(int a, int b) { | |
return a - b; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment