Link to home
Start Free TrialLog in
Avatar of mansi
mansi

asked on

CALLBACK to a Function

What is the meaning of CALLBACK to a
function in C? Is it same as a function
pointer? How & when a CALLBACK is implemented (written)?  Why are they needed? Can you please explain with an example?
Avatar of pjknibbs
pjknibbs

Quick answer: yes, a callback is just a function pointer. Callbacks can have any number of parameters and return any sort of value. I don't know where you'd use them in normal C coding, but under Windows (for example) they're used quite a lot--if you wanted to get a list of all the fonts on the system you'd call the EnumFontFamiliesEx() function and pass it a callback which would get the names of the fonts. This is useful because you have no idea how many fonts are installed before you call the function and therefore could not easily allocate space for the information.
ASKER CERTIFIED SOLUTION
Avatar of jhance
jhance

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Callback is also used to export a function from a dll. As in;
EXPORT char* CALLBACK dosomething( char* incoming_parameter)
{
// body of dosomething
return "Hello"
}
Avatar of mansi

ASKER

Dear jhance,

     some of my confusion is cleared.
but still some points are remaining about callback. I am editing the question. so Please bear with me.

                  With Thanks,
                      mansi.
mansi,

Please don't edit the original question.  This makes it confusing for others who might read this question later.

Just add followup questions as comments here.

Avatar of mansi

ASKER

Dear jhance,
      first of all sicerely sorry for the delay in responding.
Now back to callback-
      In your first response, you had written-
Strictly speaking, a callback function is any function that is
available to be called directly by the operating system.
      Now I am told that callback is like a function pointer.Now please let me know which
of the following is correct-
      1) The function pointers used by operating systems are called
as CALLBACKs. The normal function pointer used in developing process
by a programmer are called just "normal function pointers." & not CALLBACKs.
          hence technically there is no diff. between a function pointer & a CALLBACK.

      2) CALLBACK means a function pointer only or it may be a function also
some times.
      3) Is CALLBACK a reserved word in C? Can a programmer use it when
developing an application? or only a system programmer has this previlege?

      I was given the following example for explaining CALLBACK-

file: callback.h

typedef float(*fptr) (float a,float b);
void register_me(fptr func);
void print_result(float a,float b);


file : callback.c

#include<stdio.h>
#include"callback.h"
static fptr current_func = 0;
void register_me(fptr func)
{
      current_func = func;
}
void print_result(float a , float b)
{
      float c;
      if (current_func)
      {
            c=(*current_func) (a,b);
            printf("The result is %f ",c);
      }
      else
      {
            printf("\n no function to call");
      }
}
file: myapp.c

#include"callback.h"
#include"myapp.h"

int main()
{
      register_me(&subtr);
      register_me(&add);      
      print_result(a,b);
      return 0;
}

file: myapp.h

float a=25.0,b=15.0;
float add(float,float);
float subtr(float,float);

float subtr(float a,float b)
      {
            return(a-b);
      }

float add(float a,float b)
      {
            return(a+b);
      }


Is it right? Please let me know.
                                    -Mansi.
Avatar of mansi

ASKER

Adjusted points from 350 to 400