简体   繁体   中英

How Do I Read “int (*functionFactory(int n))(int, int) { … }”?

This is a syntax question. I stress that I would like to understand how to read the code below.

I am having enormous trouble trying to understand how the following code (1) translates to the code under it (2):

Code Zero:

int addInt(int n, int m) {
    return n+m;
}

Code One:

// this is a function called functionFactory which receives parameter n
// and returns a pointer to another function which receives two ints
// and it returns another int
int (*functionFactory(int n))(int, int) {
    printf("Got parameter %d", n);
    int (*functionPtr)(int,int) = &addInt;
    return functionPtr;
}

Code Two:

typedef int (*myFuncDef)(int, int);
// note that the typedef name is indeed myFuncDef

myFuncDef functionFactory(int n) {
    printf("Got parameter %d", n);
    myFuncDef functionPtr = &addInt;
    return functionPtr;
}

I am struggling with two pieces and here is why. I have modified the code above to what I believe they SHOULD look like.

Explicit Function Definition Without Typedef (Should be identical to title:

Code 4:

int (*myFuncDef)(int, int) functionFactory(int n) {
    printf("Got parameter %d", n);
    int (*functionPtr)(int,int) = &addInt;
    return functionPtr;
}

Code 5: The typedef itself (used to simplify in code 2):

typedef int (*myFuncDef)(int, int) myFuncDef;

Note that these prescribe to the basic rule: return type, idenitifer, parameters.

I would really appreciate a link to where I can read the rigorous rules of how this all works out. And an overview explanation would be great because the spec does not provide 'tutorial' like lessons. Thank you very much!

[EDIT] Also,

Note, these are excerpt from: How do function pointers in C work?

int (*functionFactory(int n))(int, int) { … }?

Remember these rules for C declares
And precedence never will be in doubt
Start with the Suffix, Proceed with the Prefix
And read both sets from the inside out

(except where parens say "do this first", of course.)

So: functionFactory is

[open paren] 
[suffix (int n)]     a function taking an `int` argument called `n` that returns
[prefix *]           a pointer to
[close paren]
[suffix (int, int)]  a function taking two `int` arguments and returning
[prefix int]         an integer

and the {...} following it gives the definition of functionFactory 's behavior.

(We might have been able to guess from the name functionFactory that it was going to return a pointer to a function. We could also have looked at its logic to see what type it was returning.)

Typedefs use exactly the same syntax as a variable declaration, with the new type name replacing the variable name and (of course) the typedef keyword in front of them. A function pointer of the type returned by this factory would have the type

int (*functionFromFactory)(int,int); /* oops, forgot parens the first time */

so a typedef for that kind of pointer would be

typedef int (*PtrToFunctionFromFactory)(int,int);

Note that once you have that type, the declaration of functionFactory could be simplified to

PtrToFunctionFromFactory functionFactory(int n) {...}

(Presumably a better name exists for this class of functions than "function from factory", and that name really should have been used in both the typedef and the name of the factory method, but since you didn't give us anything better to work with I'm sorta stuck with the overly abstract names.)

Hope that helps.

"Inside out, right to left."

From the declared name, right to left at that level is "function taking an int returning a pointer ...", out one level "... to a function taking two ints returning an int".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM