Is it possible to use function expressions in C? For example, I have the following code snippet (inside the main function):
void print_line(char *data) {
printf("%s\n", data);
}
// print all elements in my_list
list_foreach(&my_list, print_line);
I’d like to do something like this instead:
list_foreach(&my_list, void (char *data) {
printf("%s\n", data);
});
Is anything like that possible in C?
In a word, No. At least not in a Javascript-like syntax. Function pointers are as close as your are going to get. There is very little difference between the two syntactically. If you are looking for the behavior of closures or inner functions, then you definitely are not going to see them soon.