you can pass a function as a parameter- this is probably one of the coolest things you can to with any programming language
Code: Select all
#include <stdio.h>
#include <stdlib.h>
int add(int x, int y);
int sub(int x, int y);
int calc(int x, int y, int(*func)(int, int));
int add(int x, int y)
{
return x+y;
}
int sub(int x, int y)
{
return x-y;
}
int calc(int x, int y, int(*func)(int, int))
{
return func(x, y);
}
int main(void)
{
int (*fp1)(int, int) = add;
int (*fp2)(int, int) = sub;
printf("add via function poiter : %d\n", calc(2,1,fp1));
printf("sub via function poiter : %d\n", calc(2,1,fp2));
getchar();
return 0;
}