Thread: Pointers and functions.

  1. #1
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    Pointers and functions.

    Someone can explain me, what's exactly pointers to functions?
    Hmm, why we need it, and a good example to understand it?

    Really thanks for anyone that can help me!

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>Someone can explain me, what's exactly pointers to functions?
    Just that, a pointer that you can pass around and when you dereference it you hve a function :-)

    >>Hmm, why we need it, and a good example to understand it?
    A good example would be a sort function. You want to keep it as general as possible, so instead of hard coding the comparison like this
    Code:
    #include <stdio.h>
    
    void sort(int list[], size_t nelem)
    {
      size_t i, j;
    
      for (i = 0; i < nelem; i++)
      {
        for (j = 0; j < nelem; j++)
        {
          if (list[j] < list[j-1])
          {
            int temp = list[j-1];
            list[j-1] = list[j];
            list[j] = temp;
          }
        }
      }
    }
    
    int main(void)
    {
      int i;
      int a[8] = {5,6,4,7,3,8,2,9};
    
      sort(a, 8);
    
      for (i = 0; i < 8; i++)
      {
        printf("%d\n", a[i]);
      }
    
      return 0;
    }
    You make it more general so that the user can pass a pointer to a comparison function that you use
    Code:
    #include <stdio.h>
    
    int cmp(int a, int b)
      /* Return true if a is less than b */
    {
      return a < b;
    }
    
    void sort(int list[], size_t nelem, int (*compare)(int, int))
    {
      size_t i, j;
    
      for (i = 0; i < nelem; i++)
      {
        for (j = 0; j < nelem; j++)
        {
          if (compare(list[j], list[j-1]))
          {
            int temp = list[j-1];
            list[j-1] = list[j];
            list[j] = temp;
          }
        }
      }
    }
    
    int main(void)
    {
      int i;
      int a[8] = {5,6,4,7,3,8,2,9};
    
      sort(a, 8, cmp);
    
      for (i = 0; i < 8; i++)
      {
        printf("%d\n", a[i]);
      }
    
      return 0;
    }
    *Cela*

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Another usefull example of using function pointers is creating tables of function pointers. For example have a structure like this:

    Code:
    typedef struct
    {
        int sort_method;
        int (*sort_fp )(int *list, size_t nr_of_elements);
    } fp_table_item;
    Then you can create a table of function pointers:

    Code:
    fp_table_item sort_functions_table [] =
    {{INSERTION_SORT, insertion_sort},
      {QUICK_SORT, quick_sort},
      {BUBBLE_SORT, bubble_sort}};
    The parameter INSERTION_SORT is the ID of a sorting function and insertion_sort is a sorting function which sorts a list using the insertion sort algorithm. It is prototyped as returning an int and having int *list and size_t nr_of_elements as parameters.

    Now you can define a very easy handle function to handle many kinds of sorting methods.

    Code:
    int handle_sorting (int sort_method, int *list, size_t nr_of_elements)
    {
        sort_functions_table [sort_method].sort_fp (list, nr_of_elements);
    }
    Using tables with function pointers is readable and very well maintainable. It is especially usefull when there are a lot of functions, else you would have a large switch control structure. Not that that is bad, but I personally prefer using a table above an enormous switch control structure.

  4. #4
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Ok, I think I'm starting to get it, I'll try to run and understand the examples that you gave to me.

    Thanks Cela and Shiro for your help.

  5. #5
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    I found a good site that just talk about function pointer, hell, this assunt, must be an important one

    Anyway, here is the site:
    http://www.function-pointer.org/CCPP/FPT/em_fpt.html

    Maybe this can help people like me, to understand better.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 01:56 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM
  5. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 10:33 PM