Thread: The relevance of pointer to funtions

  1. #1
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751

    The relevance of pointer to funtions

    I've been searching google for some info on this but I won't be making a finite state machine any time soon and I haven't come up where I would need to assign a function as a parameter for another function. So the question is: what other uses for pointer to functions are there and have any of you used it? Are there any optimization benefits in using them?
    thanks.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by caroundw5h
    So the question is: what other uses for pointer to functions are there and have any of you used it?
    Comparing functions.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    ... if youve searcher with google you should have come across

    www.function-pointer.org

    anyway: YES!!! function pointers are VERY useful.
    for example when some function requires some cost-function.
    Code:
    int some_algorithm( struct State, (*pfn_cost)(int, int)) {
      if(pfn_cost(blah1, blah2) < pfn_cos(blah1, blah2) {
        ...
      }
      ...
    }
    if you have different cost functions you just have to pass a cost function to this - instead of writing multiple versions of your algorithm.

    also: you can map values to function

    a byte code interpreter might use this mapping - depending on the byte value the appropriate action is performed.

    (note that a switch statement would be faster but uglier here - as long as the compiler creates a jump table)

    Code:
    void f1() {
      printf("hi, im function 1\n");
    }
    
    
    void f2() {
      printf("hi, im function 2\n");
    }
    
    void (*(a_pfn_my_map[2]))();
    
    
    a_pfn_my_map[0] = &f1;
    a_pfn_my_map[1] = &f2;
    
    int i = blah;
    if(i >= 0 && i <= 2)
      a_pfn_my_map[i]();

    another useful thing about function pointers is that you might have function which all do the same - but the implementation differs.

    for example matrix multiplication can be done using the ordinary instruction set, or 3dnow or mmx extension or whatever.

    consider that:
    Code:
      if(true == mmx_support)
        matrix_mult_mmx(...);
      else if(true == 3dnow_support)
        matrix_mult_3dnow(...);
      else
        matrix_mult_fallback(...);
    your code would have hundrets of conditionals whenever youd call matrix multiply

    instead try this:

    Code:
    Matrix (*pfn_matrix_multiply)(Matrix *p_m1, Matrix *p_m2);
    
      if(true == mmx_support)
        pfn_matrix_multiply = &matrix_mult_mmx;
      else if(true == 3dnow_support)
        pfn_matrix_multiply = &matrix_mult_3dnow;
      else
        pfn_matrix_multiply = matrix_mult_fallback;
    
    
      // now we can always use the fastest implementation without having to worry which implementation actually "hides" behind the pointer
      pfn_matrix_multiply(...);

    is it clearer now?
    Last edited by Raven Arkadon; 02-27-2005 at 08:23 PM.
    signature under construction

  4. #4
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Nice!! the array of pointers to functions is nice!! Bit manipulation is where my book is taking me next perhaps once i've done that, i might see some other uses for pointers to functions. As it stands i just play around with previous code and muck around like this:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    double twice(double x);
    double half(double x);
    double thrice(double x);
    void showmenu(void);
    #define NUM 4
    int main(void)
    {
        double (*pf[NUM])(double)  = {twice, half, thrice, sqrt};
        double val;
        double ans;
        int sel;
        
        printf("Enter a number (negative to quit): ");
        while (scanf("%lf", &val) && val >= 0)
        {
            showmenu();
            while (scanf("%d", &sel) && sel >= 0 && sel <= 3)
            {
                //ans = (*pf[sel])(val);
                ans = pf[sel](val);//BSD way
                printf("answer = %f\n", ans);
                showmenu();
            }
            printf("Enter next number (negative to quit): ");
            
        }    
        puts("bye");
        return 0;
    }
    
    
    void showmenu(void)
    {
        puts("Enter one of the following choices:");
        puts("0) double the value        1) halve the value");
        puts("2) triple the value        3) squareroot the value");
        puts("4) next number");
    }
    
    double twice(double x) {return 2.0 * x;}
    double half(double x) {return x / 2.0;}
    double thrice(double x) {return 3.0 * x;}
    i feel woefuly inadequate.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  2. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. How did you master pointers?
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-17-2006, 08:23 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM