如何定义在C中返回函数数组的函数数组? [英] How do I define array of functions that return array of function in C?

查看:95
本文介绍了如何定义在C中返回函数数组的函数数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
i需要完成代码才能生效。

hi i need to complete the code so that this line will work.

f[0]()[0]();





如何定义f?



我尝试过:



这里是我试过的东西,但不知道继续





how can i define f ?

What I have tried:

here is somthing that i tried but dont know to continue

void(*a)(*f)(...)

推荐答案

[update]

没有 typedef ,符合OP请求:

[update]
Without typedef, conformant to OP request:
#include <stdio.h>

void f(){ printf("hello\n"); }
void (*pf[])()={ f };
void (**g())(){ return pf; }
void (**(*pg[])())() = { g };

int main()
{
  pg[0]()[0]();
}



[/ update]







尽可能疯狂......


[/update]



As crazy as it can be...

#include <stdio.h>

int add(int a, int b){return a+b;}
int sub(int a, int b){return a-b;}

typedef int (* INT_FUN)(int a, int b);

INT_FUN f[] = {add, sub};
INT_FUN g[] = {sub, add, sub};

INT_FUN * af (){ return f; }
INT_FUN * ag (){ return g; }

typedef INT_FUN * ( *ARR_FUN )();

int main()
{
  ARR_FUN a[] = {af, ag}; // array of functions returning array of functions...

  printf("%d\n", a[1]()[0](10,5)); // calls 'sub(10,5)'

  return 0;
}


这篇关于如何定义在C中返回函数数组的函数数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆