开发者

Function pointer doubt

Please tell me what will the call to given function return and how? The code:

typedef struct {
    int size;
    ptrdiff_t index;
    void (*inlet) ();
    int argsize;
    ptrdiff_t argindex;
} CilkProcInfo;


/*
 * Returns a pointer to the slow version for a procedure
 * whose signature is p.
 */

/* the function definition is - */
static void (*get_proc_slow(CilkProcInfo *p)) () {
     return p[0].inlet;
}

/*The function gets called as -*/
   (get_proc_slow(f->sig开发者_开发百科)) (ws, f);
/*where f->sig is a pointer to CilkProcInfo struct*/


In your CilkProcInfo structure, inlet is a pointer to a function that takes an unspecified number of arguments and does not return a value, like void foo();.

In the line

(get_proc_slow(f->sig)) (ws, f);

the get_proc_slow(f->sig) call returns this function pointer, so it is equivalent to

(f->sig[0].inlet) (ws, f);

So if your f->sig[0].inlet points to the function foo(), it is equivalent to the call

foo (ws, f);

I should admit that the static void (*get_proc_slow(CilkProcInfo *p)) () {... syntax is a bit unfamiliar to me.


get_proc_slow() returns a function pointer of type void(*)() which the code then calls. So when you do:

(get_proc_slow(f->sig)) (ws, f);

It's basically same as doing:

void (*fptr)() = get_proc_slow(f->sig);
fptr(ws, f);


It looks like it's a function that returns a pointer to a function whose return value is void that has no parameters (void(*)()) and that accepts a pointer to a CilkProcInfo struct as a parameter. I'm not sure why you'd need the p[0].inlet construct though. Couldn't you just return it as p->inlet?

Oh yeah, and get_proc_slow is the name of the function that returns said function pointer.


static void (*get_proc_slow(CilkProcInfo *p)) () {
     return p[0].inlet;
}

Reading from the name out, taking care with the grammar rules: get_proc_slow is a function (with internal linkage) that takes a pointer to a CilkProcInfo struct and returns a pointer to a function taking unspecified arguments and returning no value (void).

(get_proc_slow(f->sig)) (ws, f);

This statement calls the get_proc_slow with an appropriate parameter (f->sig is a pointer to a CilkProcInfo) and then uses the return value (a pointer to a function) to call that function with ws and f as arguments.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜