fork download
  1. #include <stdio.h>
  2.  
  3. typedef (*BinaryOperation)(int x);
  4.  
  5. BinaryOperation get_operation(int base, int opcode, int *pcontext)
  6. {
  7. int add(int *pcontext, int x)
  8. {
  9. return (*pcontext) += x;
  10. }
  11. int sub(int *pcontext, int x)
  12. {
  13. return (*pcontext) -= x;
  14. }
  15. int mult(int *pcontext, int x)
  16. {
  17. return (*pcontext) *= x;
  18. }
  19. pcontext = (int*)malloc(sizeof(int));
  20. if (!pcontext)
  21. return NULL;
  22. *pcontext = base;
  23. if (opcode == 1)
  24. return add;
  25. else if (opcode == 2)
  26. return sub;
  27. else if (opcode == 3)
  28. return mul;
  29. else
  30. return NULL;
  31. }
  32.  
  33. int main(int argc, char *argv[])
  34. {
  35. int *cnt;
  36. BinaryOperation op = get_operation(0, OP_ADD, &cnt);
  37. if (op != NULL) {
  38. printf("%d\n", op(cnt, 1));
  39. free(cnt);
  40. }
  41. return 0;
  42. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:3:11: warning: type defaults to ‘int’ in declaration of ‘BinaryOperation’ [-Wimplicit-int]
 typedef (*BinaryOperation)(int x);
           ^
prog.c: In function ‘get_operation’:
prog.c:19:2: warning: implicit declaration of function ‘malloc’ [-Wimplicit-function-declaration]
  pcontext = (int*)malloc(sizeof(int));
  ^
prog.c:19:19: warning: incompatible implicit declaration of built-in function ‘malloc’ [enabled by default]
  pcontext = (int*)malloc(sizeof(int));
                   ^
prog.c:24:3: warning: return from incompatible pointer type [enabled by default]
   return add;
   ^
prog.c:26:3: warning: return from incompatible pointer type [enabled by default]
   return sub;
   ^
prog.c:28:10: error: ‘mul’ undeclared (first use in this function)
   return mul;
          ^
prog.c:28:10: note: each undeclared identifier is reported only once for each function it appears in
prog.c: In function ‘main’:
prog.c:36:40: error: ‘OP_ADD’ undeclared (first use in this function)
  BinaryOperation op = get_operation(0, OP_ADD, &cnt);
                                        ^
prog.c:36:2: warning: passing argument 3 of ‘get_operation’ from incompatible pointer type [enabled by default]
  BinaryOperation op = get_operation(0, OP_ADD, &cnt);
  ^
prog.c:5:17: note: expected ‘int *’ but argument is of type ‘int **’
 BinaryOperation get_operation(int base, int opcode, int *pcontext)
                 ^
prog.c:38:3: warning: passing argument 1 of ‘op’ makes integer from pointer without a cast [enabled by default]
   printf("%d\n", op(cnt, 1));
   ^
prog.c:38:3: note: expected ‘int’ but argument is of type ‘int *’
prog.c:38:3: error: too many arguments to function ‘op’
prog.c:39:3: warning: implicit declaration of function ‘free’ [-Wimplicit-function-declaration]
   free(cnt);
   ^
prog.c:39:3: warning: incompatible implicit declaration of built-in function ‘free’ [enabled by default]
prog.c: In function ‘get_operation’:
prog.c:31:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
stdout
Standard output is empty