boot2

Playing with the boostrap
git clone https://git.ryansepassi.com/git/boot2.git
Log | Files | Refs | README

108-typedef-fnptr.c (556B)


      1 /* typedef of a function pointer type, used as a parameter, in a typedef
      2  * struct. */
      3 
      4 typedef int (*binop)(int, int);
      5 
      6 int do_add(int a, int b) { return a + b; }
      7 int do_mul(int a, int b) { return a * b; }
      8 
      9 int apply(binop op, int x, int y) { return op(x, y); }
     10 
     11 typedef struct {
     12     binop op;
     13     int   k;
     14 } OP;
     15 
     16 int main(int argc, char **argv) {
     17     if (apply(do_add, 3, 4) != 7)  return 1;
     18     if (apply(do_mul, 5, 6) != 30) return 2;
     19 
     20     OP rec = { do_add, 99 };
     21     if (rec.op(10, 20) != 30) return 3;
     22     if (rec.k != 99) return 4;
     23     return 0;
     24 }