Reading about function pointers, came a question, and I found some answers here at stackoverflow, but I still do not understand.
So, what is the difference between these codes ? What the compiler sees ? Is there a correct way or a good programming practice to do this ?
#include<stdio.h>
int sum(int a, int b);
void handle(int a, int b, int (*func)(int, int));
int main()
{
handle(1, 2, sum); /*Here the third argument can be sum or &sum*/
return 0;
}
void handle(int a, int b, int (*func)(int, int))
{
printf("\nResult: %d.\n\n", func(a, b)); /*Here the second argument can be func(a, b) or (*func)(a, b)*/
}
int sum(int a, int b)
{
return a + b;
}
I can call handle in two ways:
handle(1, 2, sum);
handle(1, 2, &sum);
And into handle, I can call printf in two ways:
printf("\nResult: %d.\n\n", func(a, b));
printf("\nResult: %d.\n\n", (*func)(a, b));
All these ways can be combined, so we have 4 different combined ways.
Thanks !