Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i need to complete the code so that this line will work.
f[0]()[0]();


how can i define f ?

What I have tried:

here is somthing that i tried but dont know to continue

void(*a)(*f)(...)
Posted
Updated 2-Feb-18 1:50am

1 solution

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

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

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

[/update]



As crazy as it can be...
C
#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;
}
 
Share this answer
 
v2
Comments
Richard MacCutchan 2-Feb-18 8:39am    
+5 for making me laugh.
CPallini 2-Feb-18 8:44am    
Thank you! :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900