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 6214519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:53:33+00:00 2026-05-24T06:53:33+00:00

In order to improve type safety in some C library code, I got the

  • 0

In order to improve type safety in some C library code, I got the idea to use user-definable types in callback functions. So instead of carrying a void* around, the code uses a Usertype*. The library does only declare the type, but not define it, and uses this type as an opaque pointer. Instead of the regular scheme, that a library defines all used types, the parts which are used in callback functions are forward declared, and left to be defined by the user.

/*library code*/

struct UserDataForFooCallback; /* opaque user datatype */

typedef void CallbackFn(int i, struct UserDataForFooCallback* user);

void foo(CallbackFn* callback, struct UserDataForFooCallback* user)
{
    callback(42, user);
}


/*application code*/

#include <stdio.h>
#include "foo.h"

struct UserDataForFooCallback
{
    int a;
};

static void fooCallback(int i, struct UserDataForFooCallback* user)
{
    printf(user->a == i ? "ok\n" : "fail\n");
}

int main()
{
    struct UserDataForFooCallback cbd = {42};
    foo(fooCallback, &cbd);
    return 0;
}

Advantages:

  • Type safe callbacks
  • no struct MyFoo* data=arg pointer assignments in the callback code

Drawbacks:

  • AFAIK there must not be more than one definition of a user defined datatype (or am I mixing this with the one definition rule of C++?), which makes them problematic if there are more than uses of the back-calling function (say foo in the example is used by different program parts)
  • The need to define a new type for every callback/group of callbacks (maybe it’s not so important, since the many callback functions does use one already)

The main thing I want to know if this is really a good idea, or if thie is only a good-looking-but-Armageddon-like-impact idea.

  • 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-24T06:53:33+00:00Added an answer on May 24, 2026 at 6:53 am

    If there are two libraries using your library, that both defined UserDataForFooCallback as a different structure, it is One Definition Rule violation. Just stick with void*.

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

Sidebar

Related Questions

I’m looking at ways to improve the consistency, brevity, and readability of some code
In order to improve user experience we want the ability to have an animated
In order to improve my open source project, I need testers. I have created
In order to improve performance reading from a file, I'm trying to read the
In order to fully use LinqToSql in an ASP.net 3.5 application, it is necessary
I'm build a library inspired by RSpec on top of NUnit 2.5+ in order
In order to improve performance i am creating subviews for my scroll view in
In order to perform a case-sensitive search/replace on a table in a SQL Server
In order to know how many times a pattern exists in current buffer, I
In order to debug an asp.net web app I have to have IE Script

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.