Skip to content

Instantly share code, notes, and snippets.

@Et7f3
Created November 18, 2017 22:11
Show Gist options
  • Save Et7f3/428deb9871aae05b86e7b5b53bd903d7 to your computer and use it in GitHub Desktop.
Save Et7f3/428deb9871aae05b86e7b5b53bd903d7 to your computer and use it in GitHub Desktop.
generic pointer function showing how to cast and call function by it address
#include <stdio.h>
#define FUNC_CAST(retour,function,args...) retour(*function)(args)
typedef FUNC_CAST(void,callback_t,void);
#undef FUNC_CAST
#define FUNC_CAST(retour,function,args...) ((retour(*)(args))function)
int a(int a){
return printf("%d",a);
}
int b(int a,int b){
return printf("%u %u",a,b);
}
int main()
{
callback_t p;
printf("%d %d\n",sizeof(callback_t),sizeof(short));
p=(void(*)(void))a;
((int(*)(int))p)(53);
putchar('\n');
p=(void(*)(void))b;
((int(*)(int))p)(0b11111111111111111111111111111111);
putchar('\n');
((int(*)(int,int))p)(11,12);
putchar('\n');
(*FUNC_CAST(int,&b,int,int))(13,14);
putchar('\n');
FUNC_CAST(int,p,int,int)(15,16);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment