2

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*/
2
  • 4
    A fine example of how awful the inside-out C type syntax is. Commented Oct 28, 2009 at 8:21
  • This is the reason function pointers are often declared with a typedef. Commented Oct 28, 2009 at 8:24

4 Answers 4

5

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.

Sign up to request clarification or add additional context in comments.

7 Comments

static void (*get_proc_slow(CilkProcInfo *p)) () is the same as: typedef void (proc_sig)(); static proc_sig * get_proc_slow(CilkProcInfo *p);
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?
No. void (*inlet)() is a pointer to a function taking unspecified arguments and returning no value, not no arguments.
@iWerner: You can always edit your post to include corrections.
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.
|
1

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);

Comments

0

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.

1 Comment

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".
0
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.