Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
// This file: // http://angg.twu.net/C/pointer-to-function.c.html // http://angg.twu.net/C/pointer-to-function.c // (find-angg "C/pointer-to-function.c") // Author: Eduardo Ochs <eduardoochs@gmail.com> // // See: (find-es "anatocc" "pointer-to-function") #include <stdio.h> int a=2, b=3; int f1(int x, int y) { return x + y; } int f2(int x, int y) { return x * y; } int f3(int x, int y) { return 10*x + y; } int (*f)(int, int) = f3; int main() { printf("%d\n", f3(4, 5)); /* -> 45 */ printf("%d\n", f3(a, b)); /* -> 23 */ printf("%d\n", f (a, b)); /* -> 23 */ f = f1; printf("%d\n", f(a, b)); /* -> 5 */ f = f2; printf("%d\n", f(a, b)); /* -> 6 */ f = f3; printf("%d\n", f(a, b)); /* -> 23 */ b = 7; printf("%d\n", f(a, b)); /* -> 27 */ a = 9; printf("%d\n", f(a, b)); /* -> 97 */ f = f1; printf("%d\n", f(a, b)); /* -> 16 */ } /* * (eepitch-shell) * (eepitch-kill) * (eepitch-shell) gcc -o pointer-to-function pointer-to-function.c ./pointer-to-function * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) a, b = 2, 3 f1 = function (x, y) return x + y end f2 = function (x, y) return x * y end f3 = function (x, y) return 10*x + y end f = f3 = f1 = f2 = f3 = f print(f3(4, 5)) --> 45 print(f3(a, b)) --> 23 print(f (a, b)) --> 23 f = f1; print(f(a, b)) --> 5 f = f2; print(f(a, b)) --> 6 f = f3; print(f(a, b)) --> 23 b = 7; print(f(a, b)) --> 27 a = 9; print(f(a, b)) --> 97 f = f1; print(f(a, b)) --> 16 */