0

First lets say you have functions

void foo1 (...)
{...}
void foo2 (...)
{...}
void foo3 (...)
{...}
void foo4 (...)
{...}
void foo5 (...)
{...}

Now I know we can pick a function by using either of these

void pickFunction1(char input[])
{
    if (strcmp(input, "foo1") == 0) foo1(...);
    else if (strcmp(input, "foo2") == 0) foo2(...);
    else if (strcmp(input, "foo3") == 0) foo3(...);
    else if (strcmp(input, "foo4") == 0) foo4(...);
    else if (strcmp(input, "foo5") == 0) foo5(...);
}

void pickFunction2(char input[])
{
    switch(input) {
        case 'foo1' :
            foo1(... );
            break;
        case 'foo2' :
            foo2(... );
            break;
        case 'foo3' :
            foo3(... );
            break;
        case 'foo4' :
            foo4(... );
            break;
        case 'foo5' :
            foo5(... );
            break;
}

Now my question is what happens when you have many more options, lets say you have foo1(), foo2(),....foo10000() then the previous two pickFunctions will grow linearly (O(N) where N is the number of options) with the amount of options available. Is there a way to make this linear growth of function selection into to a more constant growth function selection? Like something akin to (funcall input)in LISP?

  • "what happens when you have many more options, lets say you have foo1(), foo2(),....foo10000()" ... umm... you ... quit your job .... – bolov Oct 16 '17 at 00:24
  • 1
    refer this page for better understanding https://stackoverflow.com/questions/252748/how-can-i-use-an-array-of-function-pointers – krpra Oct 16 '17 at 00:26
  • @krpra, eh, so that was the term, I didn't know. However doesn't that only work for functions with the same type and amount of parameters? – Felipe Santana Oct 16 '17 at 00:40
  • Don't forget the calling only occurs once so you have the same parameters going in so yes the functions should strictly have the same prototype. C being what it is, you might be able to bodge some kind of subset where some of the functions have the same starting prototype but fewer actual parameters (e.g. parameters 1 & 2 are the same but some are missing 3 & 4) and cast them as you initialise the array. Only CDECL will work in this way though I think. – LoztInSpace Oct 16 '17 at 00:52

1 Answers1

0

Have a look at array of function pointer it's an array where each cells of the array is stored the adress of the function. You can fill it in a loop, same for the usage.

Nicolas Guerin
  • 356
  • 2
  • 18
  • in this way can you only fill the cells with the functions? or are you also able to add the parameters to the cell? – Felipe Santana Oct 16 '17 at 00:52
  • You fill the cells with function name and when you call the cells which points to the function, you add the parameters. example found on stackoverflow : `int sum(int a, int b); int (*p[1]) (int x, int y); int main(void) { int result; p[0] = sum;` And when you call it : `result = (*p[0]) (3, 4);` – Nicolas Guerin Oct 16 '17 at 00:55
  • @FelipeSantana If you're adding the parameters at compile time then are you talking about a constant? If so, you might want an array of structures holding the function and any known parameters. You can build your call from that. – LoztInSpace Oct 16 '17 at 00:56
  • Sorry for the syntax, it doesn't look good visualy.. `sum` is the function that do `a + b` – Nicolas Guerin Oct 16 '17 at 00:58
  • @LoztInSpace so array of function pointers but instead of functions let them be structures? – Felipe Santana Oct 16 '17 at 01:07
  • Array of structures, one member of which is the function pointer, the other members are known parameter values – LoztInSpace Oct 16 '17 at 01:18
  • Minimum members are the string to identify the function from the input and the function pointer. – Gerhardh Oct 16 '17 at 06:13