Created
August 17, 2012 15:51
-
-
Save omerk/3380077 to your computer and use it in GitHub Desktop.
C function pointer demo
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 a, int b) | |
{ | |
return a + b; | |
} | |
int addtimestwo(int (*fp)(int,int), int a, int b) | |
{ | |
return (*fp)(a, b) * 2; | |
} | |
int main(void) | |
{ | |
int a = 3, b = 5; | |
int (*fp)(int, int) = &add; | |
printf("add: %d\n", (*fp)(a, b)); | |
printf("addtimestwo: %d\n", addtimestwo(fp, a, b)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment