I have a code with a very long while loop. In this while loop, there is a long switch-case function in order to know which function should be applied on bar
.
int i=0;
while(i<BigNumber)
{
switch(variable)
{
case 1:
foo1(bar);
case 2:
foo2(bar);
etc...
}
i = i+1;
}
However, from one iteration to another of this while loop, the case in the switch-case structure is always the same. I would therefore, think that it would be more intelligent to define a unique function foo
to apply on bar
before the while loop but can I make this conditional definition of foo
? Something like
switch(variable)
{
case 1:
define foo this way
case 2:
define foo that way
etc...
}
int i=0;
while(i<BigNumber)
{
foo(bar);
i = i+1;
}
but one cannot define a function within main, so this code fails to compile. How can I solve this issue? Is there any way to define a function conditional to something? Does the best solution consists of making an array of pointers to functions and then call the correct function within the while loop by doing something like (*myarray[variable])(bar)
?
Here, function pointers are tailor-made to solve this type of challenge. You will define your normal functions foo1
, foo2
, and any more you may need. You then define a function pointer
with the syntax return type (*fpointername)(arg, list);
Your arg
, list
parameters are just the type
for the functions to be assigned. (e.g if your functions were void foo1 (int a, char b)
then you declare your function pointer as void (*fpointername)(int, char);
(variable names omitted in fn pointer definition).
The following is a simple program that illustrates the point. Drop a comment if you have any questions:
#include <stdio.h>
/* 3 void functions, one of which will become foo
* dependeing on the outcome of the switch statement
*/
void foo1 (char c) {
printf ("\n %s(%c)\n\n", __func__, c);
}
void foo2 (char c) {
printf ("\n %s(%c)\n\n", __func__, c);
}
void foodefault (char c) {
printf ("\n %s(%c)\n\n", __func__, c);
printf (" Usage: program char [a->foo1, b->foo2, other->foodefault]\n\n");
}
/* simple program passes argument to switch
* statement which assigns function to function
* pointer 'foo ()' based on switch criteria.
*/
int main (int argc, char **argv) {
void (*foo)(char) = NULL; /* function pointer initialized to NULL */
char x = (argc > 1) ? *argv[1] : 'c'; /* set 'x' value depending on argv[1] */
switch (x) /* switch on input to determine foo() */
{
case 'a' : /* input 'a' */
foo = &foo1; /* assign foo as foo1 */
break;
case 'b' : /* input 'b' */
foo = &foo2; /* assign foo as foo2 */
break;
default : /* default case assign foo foodefault */
foo = &foodefault;
}
foo (x); /* call function foo(x) */
return 0;
}
output:
$ ./bin/foofn
foodefault(c)
Usage: program char [a->foo1, b->foo2, other->foodefault]
$ ./bin/foofn a
foo1(a)
$ ./bin/foofn b
foo2(b)
$ ./bin/foofn d
foodefault(d)
Usage: program char [a->foo1, b->foo2, other->foodefault]
To elaborate on happydave's comment
void (*foo)(int);
switch(variable)
{
case 1:
foo = foo1; // or foo = &foo1; // & is optional
case 2:
foo = foo2;
etc...
}
int i=0;
while(i<BigNumber)
{
foo(bar); // (*foo)(bar); also works
i = i+1;
}