fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. typedef int (*fx_t)(int); /* fx_t e ponteiro para funcao que recebe e devolve int */
  5.  
  6. int fx1(int a) { return a + 1; }
  7. int fx2(int a) { return a + 2; }
  8. int fx3(int a) { return a + 3; }
  9.  
  10. int main(void) {
  11. char y[10];
  12. strcpy(y, "fx2"); /* obtem y do utilizador */
  13.  
  14. fx_t x = NULL;
  15. if (strcmp(y, "fx1") == 0) x = fx1;
  16. if (strcmp(y, "fx2") == 0) x = fx2;
  17. if (strcmp(y, "fx3") == 0) x = fx3;
  18. if (x) printf("%d\n", x(42)); // chama fx1(42) ou fx2(42) ou fx3(42) */
  19. return 0;
  20. }
  21.  
Success #stdin #stdout 0s 2052KB
stdin
Standard input is empty
stdout
44