Friday, November 2, 2012

Call back Functions issue !!!

Adding a call back routine: 

- It is very Simple to create call back reference to a given function , But care must be taken while defining the function body -As few compilers (old compilers though !!! ) may not give you a warning also if your function definition is different from the callback declaration and its invoking. Let us see an example which illustrates the problem.


//pointer to the function
int (*ptr_2_func)(int,int);
int test_my_function (int a, int b,int c)
//int test_my_function (int a, int b)  - This should be the function definition
  printf ("values of a is %d b is %d c is %d\n",a,b,c);
// printf ("values of a is %d b is %d \n",a,b);
 return 0;
int main()
  int ret = 0;
  ptr_2_func = test_my_function;
  ret = ptr_2_func(10,30);
  return 0;

" If we ignore the warning messages( in case your compiler is intelligent enough !!! ) , which most of the rookie programmers tend to - your program might end up crashing at some point of time if not immediate .
So its always better to make sure that your function pointer match  the function prototype.

Popular Posts