C, function pointer with predefined arguments

Is 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 would like to have a pointer to a function that “stores” the arguments. The fact is that a function pointer can be used later without requiring a reference to the source data.

I could use something like this to bind 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 it won't be as elegant as just calling a function pointer with an argument inside.

+5
source share
4 answers

, , "". C - - Apple ( , ), , , .

+3

closure curried . , C , . (Apple C , , - , 99.)

+4

GLib , . , , , ( ). (. API GLib.)

+1

, - ,

0
source

All Articles