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

前端 未结 9 2312
野趣味
野趣味 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: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.

提交回复
热议问题