Created
October 26, 2012 09:10
-
-
Save 0xeuclid/3957776 to your computer and use it in GitHub Desktop.
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
//********************************************************************** | |
//** Easy Example for describing the usage of function pointer in C ** | |
//********************************************************************** | |
#include<stdio.h> | |
//A simple funtion which return its parameter. | |
int p(int x){ return x ;} | |
typedef int (*functionPointer)(int x) ; | |
typedef struct _Interface { | |
functionPointer fp ; | |
} Interface ; | |
//A instance of Interface. | |
Interface myItf = { p } ; | |
//---MAIN: | |
int main(){ | |
// Instance usage. | |
int a = myItf.fp(10) ; | |
//A pointer to a Interface instance. | |
Interface *iface = &myItf ; | |
// Isn't it like the member funtion calling in C++ O-O language !? | |
int b = iface->fp(11) ; | |
int c = (*iface).fp(12) ; | |
printf("a=%d\n",a); | |
printf("b=%d\n",b); | |
printf("c=%d\n",c); | |
return 0; | |
} | |
//---OUTPUT: | |
/* | |
a=10 | |
b=11 | |
c=12 | |
* */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment