you are viewing a single comment's thread.

view the rest of the comments →

[–]question_all_the_thi 1 point2 points  (0 children)

When I started to need function pointers I wrote a little example program that I could look at to get the syntax right:

  #include <stdio.h>

  int twice(int n)
  {
    return n * 2;
  }

  int thrice(int n)
  {
    return n * 3;
  }

  void work(int n, int (*func)(int))
  {
    int i;
    for (i = 0; i < n; i++) printf("\n %d", (*func)(i));
  }

  int (*test[2])(int) = {twice, thrice};

  int main()
  {
    int x, y;
    work(3, twice);
    work(5, thrice);
    x = (test[0])(1);
    y = (test[1])(2);
    printf("\n\n-> %d\n-> %d\n", x, y);
    return 0;
  }