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

  • Home
  • SEARCH
  • 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 771927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:43:52+00:00 2026-05-14T18:43:52+00:00

gcc 4.4.3 c89 I am creating a client server application and I will need

  • 0

gcc 4.4.3 c89

I am creating a client server application and I will need to implement some callback functions.

However, I am not too experienced in callbacks. And I am wondering if anyone knowns some good reference material to follow when designing callbacks. Is there any design patterns that are used for c. I did look at some patterns but there where all c++.

Many thanks for any suggestions,

  • 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-14T18:43:53+00:00Added an answer on May 14, 2026 at 6:43 pm

    Here is a very rough example. Please note, the only thing I’m trying to demonstrate is the use of callbacks, its designed to be informational, not a demonstration.

    Lets say that we have a library (or any set of functions that revolve around a structure), we’re going to have code that looks similar to this (of course, I’m naming it foo):

    typedef struct foo {
         int value;
         char *text;
    } foo_t;
    

    That’s simple enough. We’d then (conventionally) provide some means of allocating and freeing it, such as:

    foo_t *foo_start(void)
    {
        foo_t *ret = NULL;
    
        ret = (foo_t *)malloc(sizeof(struct foo));
        if (ret == NULL)
            return NULL;
    
        return ret;
    }
    

    And then:

    void foo_stop(foo_t *f)
    {
       if (f != NULL)
         free(f);
    }
    

    But we want a callback, so we can define a function that will be entered when foo->text has something to report. To do that, we use a typed function pointer:

    typedef void (* foo_callback_t)(int level, const char *data);
    

    We also want any of the foo family of functions to be able to enter this callback conveniently. To do that, we need to add it to the structure, which would now look like this:

    typedef struct foo {
         int value;
         char *text;
         foo_callback_t callback;
    } foo_t;
    

    Then we write the function that will actually be entered (using the same prototype of our callback type):

    void my_foo_callback(int val, char *data)
    {
        printf("Val is %d, data is %s\n", val, data == NULL ? "NULL" : data);
    }
    

    We then need to write some convenient way to say what function it actually points to:

    void foo_reg_callback(foo_t *f, void *cbfunc)
    {
        f->callback = cbfunc;
    }
    

    And then our other foo functions can use it, for instance:

    int foo_bar(foo_t *f, char *data)
    {
        if (data == NULL)
           f->callback(LOG_ERROR, "data was NULL");
    }
    

    Note that in the above:

    f->callback(LOG_ERROR, "data was NULL");
    

    Is just like doing this:

    my_foo_callback(LOG_ERROR, "data was NULL"):
    

    Except that, we enter my_foo_callback() via a function pointer that we previously set, thereby giving us the flexibility to define our own handler on the fly (and even switch handlers if / as needed).

    One of the biggest problems with callbacks (and even the code above) is type safety when using them. A lot of callbacks will take a void * pointer, usually named something like context which could be any type of data/memory. This provides great flexibility, but can be problematic if your pointers get away from you. For instance, you don’t want to accidentally cast what is actually a struct * as char * (or int for that matter) by assignment. You can pass much more than simple strings and integers – structures, unions, enums, etc can all be passed. CCAN’s type safe callbacks help you to avoid unwittingly evil casts (to / from void *) when doing so.

    Again, this is an over simplified example that’s designed to give you an overview of one possible way to use callbacks. Please consider it psuedo code that is meant only as an example.

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

Sidebar

Related Questions

The gcc -S option will generate assembly code in AT&T syntax, is there a
GCC supports Setjump-longjump (sjlj) and Dwarf2 table-based unwinding (dw2) exception handling models. What is
GCC compiles (using gcc --omit-frame-pointer -s ): int the_answer() { return 42; } into
My gcc build toolchain produces a .map file. How do I display the memory
GNU gcc 4.3 partially supports the upcoming c++0x standard: among the implemented features the
Is it possible for gcc to link against a library that was created with
I am using gcc for windows . The OS is windows XP . How
I'm using GCC to generate a dependency file, but my build rules put the
I’m using the gcc in MinGW that comes with Strawberry Perl, on Windows XP.
I'm using GCC; __FILE__ returns the current source file's entire path and name: /path/to/file.cpp

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.