BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / cpp / #19933同步于 2009/3/6
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖

还是很扯阿,C可以定义闭包?

wks
2009/3/6镜像同步2 回复
记得上次讨论过这个问题。如果这个也可以,岂不是C语言的栈式内存分配完全被颠覆了? #include<stdio.h> typedef int (*(*second_order_function)(int))(int); typedef int (*first_order_function)(int); first_order_function getplus(int x) { int plussome(int y) { return x+y; } return plussome; } int main() { int a,b,c; while(1) { scanf("%d%d",&a,&b); c = getplus(a)(b); printf("%d\n",c); } return 0; }
订阅后,新回复会通过你的通知中心匿名送达。
2 条回复
wks机器人#1 · 2009/3/6
这个程序可以解释一部分。如果输入1 2 3 4,输出是c1=5, c2=7 #include<stdio.h> typedef int (*(*second_order_function)(int))(int); typedef int (*first_order_function)(int); first_order_function getplus(int x) { printf("getplus: x=%d\n",x); int plussome(int y) { //printf("plussome: x=%d,y=%d\n",x,y); //如果保留这个,会发生segmentfault return x+y; } return plussome; } int main() { int a1,b1,c1,a2,b2,c2; first_order_function f,g; while(1) { scanf("%d%d",&a1,&b1); scanf("%d%d",&a2,&b2); f = getplus(a1); g = getplus(a2); c1 = f(b1); c2 = g(b2); printf("c1=%d,c2=%d\n",c1,c2); } return 0; }
sunway机器人#2 · 2009/3/6
网上随便查了一下c closure, http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_4.html#SEC65 【 在 wks (cloverprince) 的大作中提到: 】 : 记得上次讨论过这个问题。如果这个也可以,岂不是C语言的栈式内存分配完全被颠覆了? : #include<stdio.h> : typedef int (*(*second_order_function)(int))(int); : ...................