/* corrected curried addition program in ANSI C */
#include <stdio.h>

typedef int (*func)(int, int);   /* binary functions on integers */
typedef struct { func f; int x; } closure;     /* closure records */
typedef closure *closurePtr;        /* pointrs to closure records */

int add(int x, int y) {return x + y; }

closurePtr cadd(int x) {
  closurePtr c;
  c = (closurePtr) malloc(sizeof(closure));   /* no check for NULL */
  c->f = add;
  c->x = x;
  return c;
}

int invoke_closure(closurePtr c, int arg) { return (c->f)(c->x, arg); }

int main() { printf("%i\n", invoke_closure(cadd(2), 3)); }
