Ich experimentierte mit c und Funktion Zeiger. Das folgende Stück Code funktioniert gut mit gcc als Compiler.Funktion Pointer in C und ihr Verhalten
typedef int(* one_var_func)(int);
int Multiply(int x, int y) {
return x * y;
}
one_var_func curry(int(* f)(int, int), int x) {
int curried_f(int y) {
return f(x, y);
}
return (curried_f);
}
int apply(int(* f)(int), int x) {
return f(x);
}
int main() {
int(* p)(int, int);
one_var_func q;
int e;
p = & Multiply;
q = curry(p, 2);
e = apply(* q, 10);
printf("%d \n", e);
return 1;
}
Allerdings, wenn ich diese geringfügige Änderung mache;
int apply(int (*f)(int) ,int x){
int a;
a=f(x)
return a;
}
Programm löst einen Segmentierungsfehler aus. Ich verstehe nicht warum und wie. Eine Erklärung wäre wirklich nett.
Ich bekomme nicht vollständig, was Sie hier versuchen, aber Sie haben eine * geschachtelte Funktion * in Ihrem Code, etwas, das nicht in C existiert. –
@FelixPalmen Es ist eine GCC-Erweiterung. Keine Ahnung, ob es wirklich zum Curry gemacht werden kann, das wirkt magisch. – unwind
Wo segfault? Was hat dein Debugger dir gesagt? –