What are function pointers used for, and how would I use them?

前端 未结 9 2206
野趣味
野趣味 2020-11-29 06:51

I understand I can use pointers for functions.

Can someone explain why one would use them, and how? Short example code would be very helpful to me.

相关标签:
9条回答
  • 2020-11-29 07:38

    A very good and easy to understand tutorial: http://www.newty.de/fpt/index.html

    Hope this helps.

    0 讨论(0)
  • 2020-11-29 07:40

    A simple case is like this: You have an array of operations (functions) according to your business logic. You have a hashing function that reduces an input problem to one of the business logic functions. A clean code would have an array of function pointers, and your program will deduce an index to that array from the input and call it.

    Here is a sample code:

    typedef void (*fn)(void) FNTYPE;
    FNTYPE fn_arr[5];
    
    fn_arr[0] = fun1; // fun1 is previously defined
    fn_arr[1] = fun2;
    ...
    
    void callMyFun(string inp) {
        int idx = decideWhichFun(inp); // returns an int between 0 and 4
        fn_arr[idx]();
    }
    

    But of course, callbacks are the most common usage. Sample code below:

    void doLengthyOperation(string inp, void (*callback)(string status)) {
      // do the lengthy task
      callback("finished");
    }
    
    void fnAfterLengthyTask(string status) {
        cout << status << endl;
    }
    
    int main() {
        doLengthyOperation(someinput, fnAfterLengthyTask);
    }
    
    0 讨论(0)
  • 2020-11-29 07:44

    Let's do a map-like function for C.

    void apply(int *arr, size_t len, int (*func)(int))
    {
        for(size_t i = 0; i < len; i++)
            arr[i] = func(arr[i]);
    }
    

    That way, we can transform a function that works on integers to work on arrays of integers. We could also do a similar version:

    void apply_enumerated(int *arr, size_t len, int (*func)(size_t, int))
    {
        for(size_t i = 0; i < len; i++)
            arr[i] = func(i, arr[i]);
    }
    

    This does the same thing, but allows our function to know which element it's on. We could use this, for example:

    int cube(int i) { return i * i * i }
    
    void print_array(int *array, size_t len, char *sep)
    {
        if(sep == NULL) sep = ", ";
        printf("%d", *array);
        for(size_t i = 1; i < len; i++) printf("%s%d", sep, array[i])
    }
    
    #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
    
    int main(void)
    {
        int array[5] = { 1, 2, 3, 4, 5 };
        print_array(array, ARRAY_SIZE(array), NULL);
        apply(array, ARRAY_SIZE(array), cube);
        print_array(array, ARRAY_SIZE(array), NULL);
        return 0;
    }
    

    That code will print:

    1, 2, 3, 4, 5
    1, 8, 27, 64, 125
    

    For our enumeration example:

    int mult(size_t i, int j) { return i * j }
    
    // print_array and ARRAY_SIZE as before
    
    int main(void)
    {
        int array[5] = { 1, 2, 3, 4, 5 };
        print_array(array, ARRAY_SIZE(array), NULL);
        apply_enumerated(array, ARRAY_SIZE(array), mult);
        print_array(array, ARRAY_SIZE(array), NULL);
        return 0;
    }
    

    This prints:

    1, 2, 3, 4, 5
    0, 2, 6, 12, 20
    

    As a more real world example, a string library could have a function that applies a function that operates on single characters to all the characters in the string. An example of such functions are the standard-library toupper() and tolower() functions in ctype.h - we could use this string_apply() function to make a string_toupper() function easily.

    0 讨论(0)
提交回复
热议问题