fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. typedef int (*fx)(int, int);
  6. int one(int a, int b) { return a + b + 1; }
  7. int two(int a, int b) { return a + b + 2; }
  8. int three(int a, int b) { return a + b + 3; }
  9. int four(int a, int b) { return a + b + 4; }
  10.  
  11. int main(void) {
  12. fx arfx[4] = {one, two, three, four};
  13. srand(time(0));
  14. for (int k = 0; k < 10; k++) {
  15. int n = rand() % 4; // 0, 1, 2, or 3
  16. int val = arfx[n](1, -1); // call one of functions in arfx
  17. printf("result is %d.\n", val);
  18. }
  19. return 0;
  20. }
Success #stdin #stdout 0s 2008KB
stdin
Standard input is empty
stdout
result is 1.
result is 1.
result is 2.
result is 3.
result is 2.
result is 1.
result is 1.
result is 4.
result is 4.
result is 4.