首页 > 代码库 > C语法归约之函数(2)
C语法归约之函数(2)
上篇给出了数组和指针的归约过程,此篇要进一步开辟新阵地——函数!主要用到文法树中的类型和抽象类型以及语句中的compound-stm;
语句和表达式的归约过程将在后期陆续给出,这里函数定义只是简单的一对花括号。
函数声明和定义归约
示例代码
本代码部分来自与C99标准中的范例
int f(void); int f(char *str, ...); int *fip(); int (*pfi)(); int (*apfi[3])(int *x, int *y); int (*fpfi(int (*)(long), int))(int, ...);//这个复杂,比较难理解,在此文最后总结部分,给出详细解释和使用实例 void addscalar(int n, int m, int a[n][n*m+300], int x);//来自与C99,不重述 int f(int (*p1)(int (*p2)(int (*p3)(void))));//在总结中给出解释 int f() { }
归约过程
curr: int next: f type-specifier declaration-specifiers curr: ID next: ( declaration-specifiers direct-declarator curr: ( next: void curr: void next: ) declaration-specifiers direct-declarator ( type-specifier declaration-specifiers direct-declarator ( declaration-specifiers declaration-specifiers direct-declarator ( parameter-declaration declaration-specifiers direct-declarator ( parameter-list declaration-specifiers direct-declarator ( parameter-type-list curr: ) next: ; declaration-specifiers direct-declarator declaration-specifiers declarator declaration-specifiers init-declarator declaration-specifiers init-declarator-list curr: ; next: int declaration declaration-list curr: int next: f declaration-list type-specifier declaration-list declaration-specifiers curr: ID next: ( declaration-list declaration-specifiers direct-declarator curr: ( next: char curr: char next: * declaration-list declaration-specifiers direct-declarator ( type-specifier declaration-list declaration-specifiers direct-declarator ( declaration-specifiers curr: * next: str declaration-list declaration-specifiers direct-declarator ( declaration-specifiers pointer curr: ID next: , declaration-list declaration-specifiers direct-declarator ( declaration-specifiers pointer direct-declarator declaration-list declaration-specifiers direct-declarator ( declaration-specifiers declarator declaration-list declaration-specifiers direct-declarator ( parameter-declaration declaration-list declaration-specifiers direct-declarator ( parameter-list curr: , next: ... curr: ... next: ) declaration-list declaration-specifiers direct-declarator ( parameter-type-list curr: ) next: ; declaration-list declaration-specifiers direct-declarator declaration-list declaration-specifiers declarator declaration-list declaration-specifiers init-declarator declaration-list declaration-specifiers init-declarator-list curr: ; next: int declaration-list declaration declaration-list curr: int next: * declaration-list type-specifier declaration-list declaration-specifiers curr: * next: fip declaration-list declaration-specifiers pointer curr: ID next: ( declaration-list declaration-specifiers pointer direct-declarator curr: ( next: ) curr: ) next: ; declaration-list declaration-specifiers pointer direct-declarator declaration-list declaration-specifiers declarator declaration-list declaration-specifiers init-declarator declaration-list declaration-specifiers init-declarator-list curr: ; next: int declaration-list declaration declaration-list curr: int next: ( declaration-list type-specifier declaration-list declaration-specifiers curr: ( next: * curr: * next: pfi declaration-list declaration-specifiers ( pointer curr: ID next: ) declaration-list declaration-specifiers ( pointer direct-declarator declaration-list declaration-specifiers ( declarator curr: ) next: ( declaration-list declaration-specifiers direct-declarator curr: ( next: ) curr: ) next: ; declaration-list declaration-specifiers direct-declarator declaration-list declaration-specifiers declarator declaration-list declaration-specifiers init-declarator declaration-list declaration-specifiers init-declarator-list curr: ; next: int declaration-list declaration declaration-list curr: int next: ( declaration-list type-specifier declaration-list declaration-specifiers curr: ( next: * curr: * next: apfi declaration-list declaration-specifiers ( pointer curr: ID next: [ declaration-list declaration-specifiers ( pointer direct-declarator curr: [ next: 3 curr: 3 next: ] declaration-list declaration-specifiers ( pointer direct-declarator [ primary-expression declaration-list declaration-specifiers ( pointer direct-declarator [ postfix-expression declaration-list declaration-specifiers ( pointer direct-declarator [ unary-expression declaration-list declaration-specifiers ( pointer direct-declarator [ cast-expression declaration-list declaration-specifiers ( pointer direct-declarator [ multiplicative-expression declaration-list declaration-specifiers ( pointer direct-declarator [ additive-expression declaration-list declaration-specifiers ( pointer direct-declarator [ shift-expression declaration-list declaration-specifiers ( pointer direct-declarator [ relational-expression declaration-list declaration-specifiers ( pointer direct-declarator [ equality-expression declaration-list declaration-specifiers ( pointer direct-declarator [ AND-expression declaration-list declaration-specifiers ( pointer direct-declarator [ exclusive-OR-expression declaration-list declaration-specifiers ( pointer direct-declarator [ inclusive-OR-expression declaration-list declaration-specifiers ( pointer direct-declarator [ logical-AND-expression declaration-list declaration-specifiers ( pointer direct-declarator [ logical-OR-expression declaration-list declaration-specifiers ( pointer direct-declarator [ conditional-expression declaration-list declaration-specifiers ( pointer direct-declarator [ assignment-expression curr: ] next: ) declaration-list declaration-specifiers ( pointer direct-declarator declaration-list declaration-specifiers ( declarator curr: ) next: ( declaration-list declaration-specifiers direct-declarator curr: ( next: int curr: int next: * declaration-list declaration-specifiers direct-declarator ( type-specifier declaration-list declaration-specifiers direct-declarator ( declaration-specifiers curr: * next: x declaration-list declaration-specifiers direct-declarator ( declaration-specifiers pointer curr: ID next: , declaration-list declaration-specifiers direct-declarator ( declaration-specifiers pointer direct-declarator declaration-list declaration-specifiers direct-declarator ( declaration-specifiers declarator declaration-list declaration-specifiers direct-declarator ( parameter-declaration declaration-list declaration-specifiers direct-declarator ( parameter-list curr: , next: int curr: int next: * declaration-list declaration-specifiers direct-declarator ( parameter-list , type-specifier declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers curr: * next: y declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers pointer curr: ID next: ) declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers pointer direct-declarator declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers declarator declaration-list declaration-specifiers direct-declarator ( parameter-list , parameter-declaration declaration-list declaration-specifiers direct-declarator ( parameter-list declaration-list declaration-specifiers direct-declarator ( parameter-type-list curr: ) next: ; declaration-list declaration-specifiers direct-declarator declaration-list declaration-specifiers declarator declaration-list declaration-specifiers init-declarator declaration-list declaration-specifiers init-declarator-list curr: ; next: int declaration-list declaration declaration-list curr: int next: ( declaration-list type-specifier declaration-list declaration-specifiers curr: ( next: * curr: * next: fpfi declaration-list declaration-specifiers ( pointer curr: ID next: ( declaration-list declaration-specifiers ( pointer direct-declarator curr: ( next: int curr: int next: ( declaration-list declaration-specifiers ( pointer direct-declarator ( type-specifier declaration-list declaration-specifiers ( pointer direct-declarator ( declaration-specifiers curr: ( next: * curr: * next: ) declaration-list declaration-specifiers ( pointer direct-declarator ( declaration-specifiers ( pointer declaration-list declaration-specifiers ( pointer direct-declarator ( declaration-specifiers ( abstract-declarator curr: ) next: ( declaration-list declaration-specifiers ( pointer direct-declarator ( declaration-specifiers direct-abstract-declarator curr: ( next: long curr: long next: ) declaration-list declaration-specifiers ( pointer direct-declarator ( declaration-specifiers direct-abstract-declarator ( type-specifier declaration-list declaration-specifiers ( pointer direct-declarator ( declaration-specifiers direct-abstract-declarator ( declaration-specifiers declaration-list declaration-specifiers ( pointer direct-declarator ( declaration-specifiers direct-abstract-declarator ( parameter-declaration declaration-list declaration-specifiers ( pointer direct-declarator ( declaration-specifiers direct-abstract-declarator ( parameter-list declaration-list declaration-specifiers ( pointer direct-declarator ( declaration-specifiers direct-abstract-declarator ( parameter-type-list curr: ) next: , declaration-list declaration-specifiers ( pointer direct-declarator ( declaration-specifiers direct-abstract-declarator declaration-list declaration-specifiers ( pointer direct-declarator ( declaration-specifiers abstract-declarator declaration-list declaration-specifiers ( pointer direct-declarator ( parameter-declaration declaration-list declaration-specifiers ( pointer direct-declarator ( parameter-list curr: , next: int curr: int next: ) declaration-list declaration-specifiers ( pointer direct-declarator ( parameter-list , type-specifier declaration-list declaration-specifiers ( pointer direct-declarator ( parameter-list , declaration-specifiers declaration-list declaration-specifiers ( pointer direct-declarator ( parameter-list , parameter-declaration declaration-list declaration-specifiers ( pointer direct-declarator ( parameter-list declaration-list declaration-specifiers ( pointer direct-declarator ( parameter-type-list curr: ) next: ) declaration-list declaration-specifiers ( pointer direct-declarator declaration-list declaration-specifiers ( declarator curr: ) next: ( declaration-list declaration-specifiers direct-declarator curr: ( next: int curr: int next: , declaration-list declaration-specifiers direct-declarator ( type-specifier declaration-list declaration-specifiers direct-declarator ( declaration-specifiers declaration-list declaration-specifiers direct-declarator ( parameter-declaration declaration-list declaration-specifiers direct-declarator ( parameter-list curr: , next: ... curr: ... next: ) declaration-list declaration-specifiers direct-declarator ( parameter-type-list curr: ) next: ; declaration-list declaration-specifiers direct-declarator declaration-list declaration-specifiers declarator declaration-list declaration-specifiers init-declarator declaration-list declaration-specifiers init-declarator-list curr: ; next: void declaration-list declaration declaration-list curr: void next: addscalar declaration-list type-specifier declaration-list declaration-specifiers curr: ID next: ( declaration-list declaration-specifiers direct-declarator curr: ( next: int curr: int next: n declaration-list declaration-specifiers direct-declarator ( type-specifier declaration-list declaration-specifiers direct-declarator ( declaration-specifiers curr: ID next: , declaration-list declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator declaration-list declaration-specifiers direct-declarator ( declaration-specifiers declarator declaration-list declaration-specifiers direct-declarator ( parameter-declaration declaration-list declaration-specifiers direct-declarator ( parameter-list curr: , next: int curr: int next: m declaration-list declaration-specifiers direct-declarator ( parameter-list , type-specifier declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers curr: ID next: , declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers declarator declaration-list declaration-specifiers direct-declarator ( parameter-list , parameter-declaration declaration-list declaration-specifiers direct-declarator ( parameter-list curr: , next: int curr: int next: a declaration-list declaration-specifiers direct-declarator ( parameter-list , type-specifier declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers curr: ID next: [ declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator curr: [ next: n curr: ID next: ] declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ primary-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ postfix-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ unary-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ cast-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ multiplicative-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ additive-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ shift-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ relational-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ equality-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ AND-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ exclusive-OR-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ inclusive-OR-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ logical-AND-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ logical-OR-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ conditional-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ assignment-expression curr: ] next: [ declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator curr: [ next: n curr: ID next: * declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ primary-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ postfix-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ unary-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ cast-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ multiplicative-expression curr: * next: m curr: ID next: + declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ multiplicative-expression * primary-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ multiplicative-expression * postfix-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ multiplicative-expression * unary-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ multiplicative-expression * cast-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ multiplicative-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ additive-expression curr: + next: 300 curr: 300 next: ] declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ additive-expression + primary-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ additive-expression + postfix-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ additive-expression + unary-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ additive-expression + cast-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ additive-expression + multiplicative-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ additive-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ shift-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ relational-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ equality-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ AND-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ exclusive-OR-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ inclusive-OR-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ logical-AND-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ logical-OR-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ conditional-expression declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator [ assignment-expression curr: ] next: , declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers declarator declaration-list declaration-specifiers direct-declarator ( parameter-list , parameter-declaration declaration-list declaration-specifiers direct-declarator ( parameter-list curr: , next: int curr: int next: x declaration-list declaration-specifiers direct-declarator ( parameter-list , type-specifier declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers curr: ID next: ) declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers direct-declarator declaration-list declaration-specifiers direct-declarator ( parameter-list , declaration-specifiers declarator declaration-list declaration-specifiers direct-declarator ( parameter-list , parameter-declaration declaration-list declaration-specifiers direct-declarator ( parameter-list declaration-list declaration-specifiers direct-declarator ( parameter-type-list curr: ) next: ; declaration-list declaration-specifiers direct-declarator declaration-list declaration-specifiers declarator declaration-list declaration-specifiers init-declarator declaration-list declaration-specifiers init-declarator-list curr: ; next: int declaration-list declaration declaration-list curr: int next: f declaration-list type-specifier declaration-list declaration-specifiers curr: ID next: ( declaration-list declaration-specifiers direct-declarator curr: ( next: int curr: int next: ( declaration-list declaration-specifiers direct-declarator ( type-specifier declaration-list declaration-specifiers direct-declarator ( declaration-specifiers curr: ( next: * curr: * next: p1 declaration-list declaration-specifiers direct-declarator ( declaration-specifiers ( pointer curr: ID next: ) declaration-list declaration-specifiers direct-declarator ( declaration-specifiers ( pointer direct-declarator declaration-list declaration-specifiers direct-declarator ( declaration-specifiers ( declarator curr: ) next: ( declaration-list declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator curr: ( next: int curr: int next: ( declaration-list declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( type-specifier declaration-list declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( declaration-specifiers curr: ( next: * curr: * next: p2 declaration-list declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( declaration-specifiers ( pointer curr: ID next: ) declaration-list declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( declaration-specifiers ( pointer direct-declarator declaration-list declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( declaration-specifiers ( declarator curr: ) next: ( declaration-list declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator curr: ( next: int curr: int next: ( declaration-list declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( type-specifier declaration-list declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( declaration-specifiers curr: ( next: * curr: * next: p3 declaration-list declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( declaration-specifiers ( pointer curr: ID next: ) declaration-list declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( declaration-specifiers ( pointer direct-declarator declaration-list declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( declaration-specifiers ( declarator curr: ) next: ( declaration-list declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator curr: ( next: void curr: void next: ) declaration-list declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( type-specifier declaration-list declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( declaration-specifiers declaration-list declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( parameter-declaration declaration-list declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( parameter-list declaration-list declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( parameter-type-list curr: ) next: ) declaration-list declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator declaration-list declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( declaration-specifiers declarator declaration-list declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( parameter-declaration declaration-list declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( parameter-list declaration-list declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( parameter-type-list curr: ) next: ) declaration-list declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator declaration-list declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( declaration-specifiers declarator declaration-list declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( parameter-declaration declaration-list declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( parameter-list declaration-list declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator ( parameter-type-list curr: ) next: ) declaration-list declaration-specifiers direct-declarator ( declaration-specifiers direct-declarator declaration-list declaration-specifiers direct-declarator ( declaration-specifiers declarator declaration-list declaration-specifiers direct-declarator ( parameter-declaration declaration-list declaration-specifiers direct-declarator ( parameter-list declaration-list declaration-specifiers direct-declarator ( parameter-type-list curr: ) next: ; declaration-list declaration-specifiers direct-declarator declaration-list declaration-specifiers declarator declaration-list declaration-specifiers init-declarator declaration-list declaration-specifiers init-declarator-list curr: ; next: int declaration-list declaration declaration-list curr: int next: f declaration-list type-specifier declaration-list declaration-specifiers curr: ID next: ( declaration-list declaration-specifiers direct-declarator curr: ( next: ) curr: ) next: { declaration-list declaration-specifiers direct-declarator declaration-list declaration-specifiers declarator curr: { next: } curr: } next: NULL declaration-list declaration-specifiers declarator compound-statement declaration-list function-definition declaration-list function-definition-list translation-unit
总结
int (*pfi)();//这是个函数指针 int (*apfi[3])(int *x, int *y); //这是个函数指针数组,三个函数类型是int (*)(int *, int *)指针变量它的最大应用就是钩子函数!假设三个功能函数接口类型一致,且有相对编号012,那么指针数组就派上用场了,使用编号调用接口等等吧,这只是常见的应用一种方式!
int (*fpfi(int (*)(long), int))(int, ...);从最里层开始看起,int (*)(long)这是个抽象函数类型,它指定了函数fpfi的第一个参数的类型,即fpfi()函数第一个参数是个函数指针,紧接是个int,即fpfi()第二参数是个整型变量。fpfi()函数的返回值类型是int (*)(int, ...),这样就清楚了!
函数名:fpfi
返回值:int(*)(int, ...)
参数:P1:int(*)(long); p2:int
为了便于理解,写个简单范例,注意这个范例很黄很暴力!哈哈
#include <stdio.h> int (*mother(int (*)(long), int))(int); int girl(long leg) { printf("girl's leg? %ld\n", leg); return -1; } int boy(int big) { printf("boy's pipe ? %d\n", big); return -2; } int (*mother(int (*g)(long), int m)) (int) { g(35); printf("mother marry%d\n", m); return boy; } int main(int argc, char *argv[]) { int (*father)(int); printf("first:\n"); father = mother(girl, 10); father(19); int (*(*wife)(int (*)(long), int))(int); printf("second:\n"); wife = mother; father = wife(girl, 15); father(21); return 0; }
int f(int (*p1)(int (*p2)(int (*p3)(void))));函数f的参数类型是 int (*)(int (*)(int (*)())),意思就是(*p2)的参数是个函数指针int (*)(),(*p1)的参数还是个函数指针 int (*)(int (*)())
下篇:表达式
C语法归约之函数(2)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。