views:

162

answers:

5

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*/
+1  A: 

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);
reko_t
+4  A: 

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.

iWerner
static void (*get_proc_slow(CilkProcInfo *p)) () is the same as: typedef void (proc_sig)(); static proc_sig * get_proc_slow(CilkProcInfo *p);
Adrien Plisson
I am a bit confused.. if foo() doesn't take any parameters how you could pass ws and f as params? Isn't that a compiler error?
Naveen
No. `void (*inlet)()` is a pointer to a function taking _unspecified_ arguments and returning no value, not _no_ arguments.
Charles Bailey
I stand corrected wrt Charles' comment.
iWerner
@iWerner: You can always edit your post to include corrections.
Charles Bailey
Isn't there a type mismatch between what is returned by the function and what is in declaration? I mean that the declaration says - void, but it actually returns a pointer to a function.
Swapnil
@Swapnil: No, the function isn't returning `void`, it's returning pointer to function returning `void`. There's no mismatch.
Charles Bailey
But in this code - static void (*get_proc_slow(CilkProcInfo *p)) () { return p[0].inlet;} The declaration says that it should return void. And the return statement says that it should return a pointer to a function returning void. Why is it not a type mismatch?
Swapnil
A: 

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.

zdawg
As it has already been noted in other comments, `()` parameter list in C does not mean "has no parameters`. It means "unspecified number of parameters".
AndreyT
A: 
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.

Charles Bailey
A: 

Thank you iWerner and Charles. You were very helpful. The question is answered.

Swapnil