Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6326269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:03:29+00:00 2026-05-24T17:03:29+00:00

Having read a bit about function pointers and callbacks, I fail to understand the

  • 0

Having read a bit about function pointers and callbacks, I fail to understand the basic purpose of it. To me it just looks like instead of calling the function directly we use the pointer to that function to invoke it. Can anybody please explain me callbacks and function pointers? How come the callback takes place when we use function pointers, because it seems we just call a function through a pointer to it instead of calling directly?

Thanks

ps: There have been some questions asked here regarding callbacks and function pointers but they do not sufficiently explain my problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T17:03:29+00:00Added an answer on May 24, 2026 at 5:03 pm

    What is a Callbak function?
    In simple terms, a Callback function is one that is not called explicitly by the programmer. Instead, there is some mechanism that continually waits for events to occur, and it will call selected functions in response to particular events.
    This mechanism is typically used when a operation(function) can take long time for execution and the caller of the function does not want to wait till the operation is complete, but does wish to be intimated of the outcome of the operation. Typically, Callback functions help implement such an asynchronous mechanism, wherein the caller registers to get inimated about the result of the time consuming processing and continuous other operations while at a later point of time, the caller gets informed of the result.

    An practical example:
    Windows event processing:
    virtually all windows programs set up an event loop, that makes the program respond to particular events (eg button presses, selecting a check box, window getting focus) by calling a function. The handy thing is that the programmer can specify what function gets called when (say) a particular button is pressed, even though it is not possible to specify when the button will be pressed. The function that is called is referred to as a callback.

    An source Code Illustration:

    //warning:  Mind compiled code, intended to illustrate the mechanism    
    #include <map>
    
    typedef void (*Callback)();
    std::map<int, Callback>  callback_map;
    
    void RegisterCallback(int event, Callback function)
    {
        callback_map[event] = function;
    }
    
    bool finished = false;
    
    int GetNextEvent()
    {
        static int i = 0;
        ++i;
        if (i == 5) finished = false;
    }
    
    void EventProcessor()
    {
        int event;
        while (!finished)
        {
            event = GetNextEvent();
            std::map<int, Callback>::const_iterator it = callback_map.find(event);
            if (it != callback_map.end())    // if a callback is registered for event
            {
                Callback function = *it;
                if (function)   
                {
                    (*function)();
                }
                else
                {
                   std::cout << "No callback found\n";
                }
            }
        }
    }
    
    void Cat()
    {
       std::cout << "Cat\n";
    }
    
    void Dog()
    {
        std::cout << "Dog\n";
    }
    
    void Bird()
    {
        std::cout << "Bird\n";
    }
    
    int main()
    {
        RegisterCallBack(1, Cat);
        RegisterCallback(2, Dog);
        RegisterCallback(3, Cat);
        RegisterCallback(4, Bird);
        RegisterCallback(5, Cat);
    
        EventProcessor(); 
        return 0;
    }
    

    The above would output the following:

    Cat  
    Dog   
    Cat  
    Bird  
    Cat  
    

    Hope this helps!


    Note: This is from one of my previous answers, here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was just having a quick read through this article (specifically the bit about
So I read a bit about CUDA and GPU programming. I noticed a few
I've read in forums about people having issues with certain data types when accessing
I've read quite a bit about initialization of static class variables in c++ and
I have read quite a bit about the ReadOnly databases. Therefore I was wondering
Having read the threads Is SqlCommand.Dispose enough? and Closing and Disposing a WCF Service
Having read up on quite a few articles on Artificial Life (A subject I
Having read an existing post on stackoverflow and done some reading around on the
Having read Fowler's Refactoring for a while, I still often catch myself thinking I
Having read Joe Armstrong's book and watched Kevin Smith screencasts I've built a simple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.