cvariablespointerstypesdeclaration

type of int * (*) (int * , int * (*)())


int * (*) (int * , int * (*)())

I'd like to know what type is it ? , can someone give an example of a declaration using this type.

any help would be great.

thanks.


Solution

  • It is a pointer to function that returns int* and accepts int* and pointer to function that returns int* (and accepts undefined number of parameters; see comments).

    Some example (does not look very nice, it is just constructed to contain the mentioned declaration):

    #include <stdio.h>
    
    static int a = 10;
    int* f1() {
        return &a;
    }
    
    static int b;
    int* f2(int *j, int*(*f)()) {
        b = *j + *f();
        // this is just for demonstrational purpose, such usage
        // of global variable makes this function not thread-safe
        return &b;
    } 
    
    
    int main(int argc, char *argv[]) {
        int * (*ptr1)();
        int * (*ptr2) (int * , int * (*)());
        ptr1 = f1;
        ptr2 = f2;
    
        int i = 42;
        int *pi = ptr2(&i, ptr1);
        printf("%d\n", *pi);
    
        return 0;
    }
    
    // prints 52