【发布时间】:2014-03-07 12:02:55
【问题描述】:
这是一个语法问题。我强调我想了解如何阅读下面的代码。
我在试图理解以下代码 (1) 如何转换为它下面的代码 (2) 时遇到了巨大的麻烦:
代码零:
int addInt(int n, int m) {
return n+m;
}
代码一:
// 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;
}
代码二:
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;
}
我正在为两件事情苦苦挣扎,这就是原因。我已将上面的代码修改为我认为它们应该的样子。
没有Typedef的显式函数定义(应该与标题相同:
代码 4:
int (*myFuncDef)(int, int) functionFactory(int n) {
printf("Got parameter %d", n);
int (*functionPtr)(int,int) = &addInt;
return functionPtr;
}
代码 5: typedef 本身(用于简化代码 2):
typedef int (*myFuncDef)(int, int) myFuncDef;
请注意,这些规定了基本规则:返回类型、标识符、参数。
我非常感谢一个链接,我可以在其中阅读有关这一切如何运作的严格规则。概述解释会很好,因为spec 不提供类似“教程”的课程。 非常感谢!
[编辑]另外,
【问题讨论】:
-
我使用了那个网站,但我不想依赖它。我希望自己能够阅读。但我找不到严格描述它如何工作的规范。
-
重复,一个可能永远不应该问的问题,因为这样的代码的读者只会有同样的问题。此外,如果您希望能够在 C 中解析 anything,请参阅 the current standard。
标签: c