5

Is something like this possible in C?

#include <stdio.h>

void print_str(char *str) {
        printf(str);
}

int main() {

        void (*f_ptr)() = print_str,"hello world";

        f_ptr();

}

//see "hello world" on stdout

In short, I'd like to have a function pointer that "stores" the arguments. The point is that the function pointer can be used later on without needing a reference to the original data.

I could use something like this to couple a function pointer and an argument reference

struct f_ptr {
 void (*f)();
 void *data;
}

void exec_f_ptr(f_ptr *data) {
  data->f(data->data):
}

but wouldn't be as elegant as just calling a function pointer with the argument inside.

4
  • printf(str) is asking for format string attack! Commented Jun 30, 2010 at 4:26
  • 1
    You're using the wrong language. The second solution is generally what is used in C. We can't just take function pointers with different signatures and try to call them all through the same code. C++ has more facilities and is better suited for this, but scripting languages with dynamic typing and functions as first-class objects are generally best suited for this kind of code. Commented Jun 30, 2010 at 4:30
  • You could define a macro #define DO_FUN( d ) (d)->f((d)->data), then to call a pointer with the argument, you turn your one line into DO_FUN(data); Commented Jun 30, 2010 at 4:42
  • Oh, that is what you are doing with the exec function, I see now? In that case, everyone is right, this is pretty much the best you've got. Commented Jun 30, 2010 at 4:44

4 Answers 4

4

What you want is a closure or a curried function. Unfortunately, C has neither of these. (Apple did introduce closures in its version of C and hopefully they'll be adopted for some future version of the language, but it's not part of C99.)

Sign up to request clarification or add additional context in comments.

1 Comment

Is a curried function a closure?
3

You're basically asking for a closure rather than a function pointer--that is, data and code in one "object." Such objects don't exist in standard C--you can get something similar from Apple's blocks or from anonymous functions in other languages (or from closures outright in the languages that support them) but generally speaking you'll have to construct some data type of your own, as you've discovered.

1 Comment

For a large implementation of this structure, the Function pointer and argument, I believe that DIKU and ROM MUD's had this style of structure passing around for a lot of the functions.
1

GLib has support for closures, used mainly for signal callbacks. It's cross platform, and might be worth a look (depending on your requirements). (See also the GLib closure API.)

1 Comment

While interesting, this probably isn't very useful since the normal place you want closures in C is to use with legacy APIs that omit the ability to pass a context themselves - for example, if you want to use qsort() but the comparison depends on a parameter and there are enough possible parameters that you can't make a different function for each one.
0

No, that struct is the closest thing you're going to get

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.