Search code examples
c++cfunction-pointerstypedeffunction-calls

Can't understand code/function call


The declarations related to the question are are :

typedef void (*struct_c)(
    pid_t,  
    const uint_t *,
    struct_a,       
    struct_a,       
    void *);

void func1(struct_a s, struct_a e, struct_d init, struct_c range, struct_e fini, void *arg);
static void add_range(pid_t mgid, const uint_t *propids,struct_a s, struct_a e, void *arg);

There is a function call as follows:

func1(s, e,NULL,add_range, NULL, &ranges);

The argument add_range is a function name, there is no other variable by that name.

I don't understand how the function call func1 works and what are its arguments.

If you need more details,let me know.


Solution

  • struct_c is a function pointer

    void func1(struct_a s, struct_a e, struct_d init, struct_c range, struct_e fini, void *arg); says

    contains the function pointer in its 3rd argument i.e. struct_c range which matches the prototype of the function pointer

    typedef void (*struct_c)(
        pid_t,  
        const uint_t *,
        struct_a,       
        struct_a,       
        void *);
    

    and the prototype of the add_range function.

    static void add_range(pid_t mgid, const uint_t *propids,struct_a s, struct_a e, void *arg);

    In a trivial sense,

    Function pointers are to functions, as, integer pointers are to integers or such.