Tuesday, January 08, 2008

C: Pointers to function

Check the following code and comments

void func(int);
int main()
{
void (*fp)(int);
fp = func; // following two are the same
fp = &func;
(*fp)(1); // same result
fp(2);
return 0;
}
void func(int a)
{
printf ("%d\n", a);
}

c-faq explains the details in 4.12 and 1.34. Note that either func(3) or (*func)(3) is legal too.

Also check The Function Pointer Tutorials

No comments:

Post a Comment