-1

I have a project in C. In the function adminpanel() there are some selections such as add, delete, modify etc. Each of these selections is a function.Now, for example i call the add() function and when it ends i need to return at the begging on the adminpanel() function to make another selection. This happens with the delete() etc. Is it possible to set as return value in the function add(), delete() etc. the function adminpanel() ? If not, what i have to set as a return value?

Xen
  • 35
  • 6

1 Answers1

2

It is possible to return a function pointer:

int inc( int x ) { return x + 1; }
int dec( int x ) { return x - 1; }

int( * func( int t ) ) ( int )
{
    return t ? &inc : &dec;
}

int test_C( void )
{
    int( *f1 )( int ) = func( 1 ); // f1 = &inc
    int x1 = ( *f1 )( 1 );         // x1 = 2            

    int( *f2 )( int ) = func( 0 ); // f2 = &dec
    int x2 = ( *f2 )( 1 );         // x2 = 0    

    return 0;
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174