简体   繁体   English

如何阅读“ int(* functionFactory(int n))(int,int){…}”?

[英]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): 我在尝试理解以下代码(1)如何转换为其下的代码(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: 没有Typedef的显式函数定义(应与title相同:

Code 4: 代码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): 代码5:typedef本身(用于简化代码2):

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

Note that these prescribe to the basic rule: return type, idenitifer, parameters. 请注意,这些规定了基本规则: 返回类型,idenitifer,参数。

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, [EDIT]另外,

Note, these are excerpt from: How do function pointers in C work? 注意,这些摘录来自: C中的函数指针如何工作?

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 所以:functionFactory是

[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. 其后的{...}给出了functionFactory行为的定义。

(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.) (我们可能已经从名称functionFactory猜测出它将要返回指向函数的指针。我们也可能已经查看了其逻辑以查看其返回的类型。)

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. Typedef使用与变量声明完全相同的语法,新的类型名称替换了变量名称,并且(当然)替换了它们前面的typedef关键字。 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将是

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

Note that once you have that type, the declaration of functionFactory could be simplified to 请注意,一旦有了该类型,就可以将functionFactory的声明简化为

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.) (假定此类函数的名称比“工厂中的函数”更好,而且该名称确实应该在typedef和factory方法的名称中使用,但是由于您没有给我们任何更好的工作方法,我有点过于抽象了。)

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". 从声明的名称开始,在该级别的从右到左是“将一个int返回指针的函数...”,从一个级别“ ...到将两个int返回指针的函数”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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