Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. callbacks in c

callbacks in c

Scheduled Pinned Locked Moved C / C++ / MFC
tutoriallearning
3 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member_15069718
    wrote on last edited by
    #1

    Hi All, I am learning callbacks in c.I found an example in callback but could not understand .Can some one please explain me how the flow of execution happens and brief explaination of program. PFA code what I found.

    * callback.c */
    #include
    #include"reg_callback.h"

    /* callback function definition goes here */
    void my_callback(void)
    {
    printf("inside my_callback\n");
    }

    int main(void)
    {
    /* initialize function pointer to
    my_callback */
    callback ptr_my_callback=my_callback;
    printf("This is a program demonstrating function callback\n");
    /* register our callback function */
    register_callback(ptr_my_callback);
    printf("back inside main program\n");
    return 0;
    }

    /* reg_callback.h */
    typedef void (*callback)(void);
    void register_callback(callback ptr_reg_callback);
    /* reg_callback.c */
    #include
    #include"reg_callback.h"

    /* registration goes here */
    void register_callback(callback ptr_reg_callback)
    {
    printf("inside register_callback\n");
    /* calling our callback function my_callback */
    (*ptr_reg_callback)();
    }

    Greg UtasG K 2 Replies Last reply
    0
    • M Member_15069718

      Hi All, I am learning callbacks in c.I found an example in callback but could not understand .Can some one please explain me how the flow of execution happens and brief explaination of program. PFA code what I found.

      * callback.c */
      #include
      #include"reg_callback.h"

      /* callback function definition goes here */
      void my_callback(void)
      {
      printf("inside my_callback\n");
      }

      int main(void)
      {
      /* initialize function pointer to
      my_callback */
      callback ptr_my_callback=my_callback;
      printf("This is a program demonstrating function callback\n");
      /* register our callback function */
      register_callback(ptr_my_callback);
      printf("back inside main program\n");
      return 0;
      }

      /* reg_callback.h */
      typedef void (*callback)(void);
      void register_callback(callback ptr_reg_callback);
      /* reg_callback.c */
      #include
      #include"reg_callback.h"

      /* registration goes here */
      void register_callback(callback ptr_reg_callback)
      {
      printf("inside register_callback\n");
      /* calling our callback function my_callback */
      (*ptr_reg_callback)();
      }

      Greg UtasG Offline
      Greg UtasG Offline
      Greg Utas
      wrote on last edited by
      #2

      void my_callback(void)... /* This is the callback function that will be invoked. */

      int main(void)
      {
      callback ptr_my_callback = my_callback; /* ptr_my_callback points to the function my_callback */
      register_callback(ptr_my_callback); /* registers the callback */
      }

      /* This defines the type "callback". The '*' before "callback", which is in
      parentheses, means that it's a pointer to a function. The function returns
      void and takes (void) as its parameter. */

      typedef void (*callback)(void);

      /* This function is called to register the callback. It actually doesn't do
      that, because registering would mean saving a pointer to it. All that it does
      is invoke the function immediately, which is what it could do later if it
      saved its parameter somewhere and used it to call the function when it was
      time to do so. To call the function, it dereferences the pointer to it and
      invokes it with no arguments. */

      void register_callback(callback ptr_reg_callback)
      {
      (*ptr_reg_callback)();
      }

      The printfs are just there so that you can observe the flow of execution.

      Robust Services Core | Software Techniques for Lemmings | Articles
      The fox knows many things, but the hedgehog knows one big thing.

      <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
      <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

      1 Reply Last reply
      0
      • M Member_15069718

        Hi All, I am learning callbacks in c.I found an example in callback but could not understand .Can some one please explain me how the flow of execution happens and brief explaination of program. PFA code what I found.

        * callback.c */
        #include
        #include"reg_callback.h"

        /* callback function definition goes here */
        void my_callback(void)
        {
        printf("inside my_callback\n");
        }

        int main(void)
        {
        /* initialize function pointer to
        my_callback */
        callback ptr_my_callback=my_callback;
        printf("This is a program demonstrating function callback\n");
        /* register our callback function */
        register_callback(ptr_my_callback);
        printf("back inside main program\n");
        return 0;
        }

        /* reg_callback.h */
        typedef void (*callback)(void);
        void register_callback(callback ptr_reg_callback);
        /* reg_callback.c */
        #include
        #include"reg_callback.h"

        /* registration goes here */
        void register_callback(callback ptr_reg_callback)
        {
        printf("inside register_callback\n");
        /* calling our callback function my_callback */
        (*ptr_reg_callback)();
        }

        K Offline
        K Offline
        k5054
        wrote on last edited by
        #3

        You might already be familiar with callbacks and not realize it. If you've ever used qsort() or bsearch(), or another library function that takes a function pointer as an argument to help it do its work, you have already used callbacks! For example the signature for qsort is

        void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));

        and might be used like this

        struct foo {
        char key[10];
        int id;
        /* more members */
        };

        /* comparison function to sort struct foo by key */
        int cmpfoo_bykey(const void *vp1, const void *vp2)
        {
        const struct foo *foo1 = vp1;
        const struct foo *foo2 = vp2;

        return strcmp(foo1->key, foo2->key);
        }

        /* comparison function to sort struct foo by id */
        int cmpfoo_byid(const void *vp1, const void *vp2)
        {
        const struct foo *foo1 = vp1;
        const struct foo *foo2 = vp2;

        return foo1->id - foo2->id;
        }

        int main()
        {
        struct foo my_data[100];
        /* load data into my_data : not show here*/

        /* sort my_data by key field */
        qsort(my_data, 100, sizeof(struct foo), cmpfoo_bykey);

        /* use my_data in key-ordered way ... */

        /* sort my_data by id field */
        qsort(my_data, 100, sizeof(struct foo), cmpfoo_byid);

        /* use my_data in id-ordered way ... */

        return 0;
        }

        In the above example qsort() knows how to sort an array of items in a generic way, but not how to compare items. It gets around this limitation by using a callback function, that the programmer provides, that does know how to compare items in the array. So, whenever qsort() needs to know how two items in the array compare to each other, it calls back to the code provided by the programmer to determine how the two items compare to each other.

        Keep Calm and Carry On

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups