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?
Asked
Active
Viewed 214 times
-1
-
1No. You can't return a function or arrays from a function. It's possible to return a pointer to a function. – haccks Jan 02 '16 at 11:49
-
@T.J.Crowder; No. Not a dupe of that. – haccks Jan 02 '16 at 11:50
-
@haccks: The standard for "is this a dupe" is "Does an answer to the other question answer this question." So yes, it's a dupe. (If I were making the rules, the rule would probably be different.) – T.J. Crowder Jan 02 '16 at 11:52
-
@T.J.Crowder: So if you do not like the rules, why stick to them? ;-) – alk Jan 02 '16 at 11:58
-
@alk: Because otherwise, chaos. – T.J. Crowder Jan 02 '16 at 12:01
1 Answers
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