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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:43:14+00:00 2026-06-17T06:43:14+00:00

I’d like to be able to generically pass a function to a function in

  • 0

I’d like to be able to generically pass a function to a function in C. I’ve used C for a few years, and I’m aware of the barriers to implementing proper closures and higher-order functions. It’s almost insurmountable.

I scoured StackOverflow to see what other sources had to say on the matter:

  • higher-order-functions-in-c
  • anonymous-functions-using-gcc-statement-expressions
  • is-there-a-way-to-do-currying-in-c
  • functional-programming-currying-in-c-issue-with-types
  • emulating-partial-function-application-in-c
  • fake-anonymous-functions-in-c
  • functional-programming-in-c-with-macro-higher-order-function-generators
  • higher-order-functions-in-c-as-a-syntactic-sugar-with-minimal-effort

…and none had a silver-bullet generic answer, outside of either using varargs or assembly. I have no bones with assembly, but if I can efficiently implement a feature in the host language, I usually attempt to.

Since I can’t have HOF easily…

I’d love higher-order functions, but I’ll settle for delegates in a pinch. I suspect that with something like the code below I could get a workable delegate implementation in C.

An implementation like this comes to mind:

enum FUN_TYPES {
    GENERIC,
    VOID_FUN,
    INT_FUN,
    UINT32_FUN,
    FLOAT_FUN,
};

typedef struct delegate {
    uint32 fun_type;
    union function {
        int (*int_fun)(int);
        uint32 (*uint_fun)(uint);
        float (*float_fun)(float);
        /* ... etc. until all basic types/structs in the 
           program are accounted for. */
    } function;
} delegate;

Usage Example:

void mapint(struct fun f, int arr[20]) {
    int i = 0;
    if(f.fun_type == INT_FUN) {
        for(; i < 20; i++) {
            arr[i] = f.function.int_fun(arr[i]);
        }
    }
}

Unfortunately, there are some obvious downsides to this approach to delegates:

  • No type checks, save those which you do yourself by checking the ‘fun_type’ field.
  • Type checks introduce extra conditionals into your code, making it messier and more branchy than before.
  • The number of (safe) possible permutations of the function is limited by the size of the ‘fun_type’ variable.
  • The enum and list of function pointer definitions would have to be machine generated. Anything else would border on insanity, save for trivial cases.
  • Going through ordinary C, sadly, is not as efficient as, say a mov -> call sequence, which could probably be done in assembly (with some difficulty).

Does anyone know of a better way to do something like delegates in C?

Note: The more portable and efficient, the better

Also, Note: I’ve heard of Don Clugston’s very fast delegates for C++. However, I’m not interested in C++ solutions–just C .

  • 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-06-17T06:43:16+00:00Added an answer on June 17, 2026 at 6:43 am

    You could add a void* argument to all your functions to allow for bound arguments, delegation, and the like. Unfortunately, you’d need to write wrappers for anything that dealt with external functions and function pointers.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I

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.