Skip to content

Instantly share code, notes, and snippets.

@virtualsafety
Forked from 0xeuclid/function pointer.c
Created July 26, 2022 10:41
Show Gist options
  • Save virtualsafety/4bb9a82bfceeecf906ba522c75b4d5d6 to your computer and use it in GitHub Desktop.
Save virtualsafety/4bb9a82bfceeecf906ba522c75b4d5d6 to your computer and use it in GitHub Desktop.
//**********************************************************************
//** 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