4

Lets say i've got functions named Function1,Function2,Function3 etc. Is there a way of calling one of the functions in a loop each time?

for(i=1;i<Max;i++)
{
Function^();
}
2
  • 1
    Are the number of functions and their names known at compile time? Commented Aug 1, 2015 at 15:58
  • Yes....all the function names are known Commented Aug 1, 2015 at 16:00

3 Answers 3

8

Try this:

#include <stdio.h>

void func1(void)
{
    printf( "func1\n" );
}

void func2(void)
{
    printf( "func2\n" );
}

void func3(void)
{
    printf( "func3\n" );
}

typedef void ( *func )(void);

int main(void)
{
    func m_func[3] = {func1, func2, func3};
    int index;

    for( index = 0; index < 3; index++ )
    {
        m_func[index]();
    }

    return 0;
}
Sign up to request clarification or add additional context in comments.

9 Comments

@PaulR: C11 draft standard Section 5.1.2.2.3 Program termination [...] reaching the } that terminates the main function returns a value of 0. [...]
That does what i want ...but i was hoping there is a way of avoiding to at some point list all the functions....func1,func2,func3...
@RangoTango: I am afraid it cannot be done. stackoverflow.com/questions/16216585/…
@PaulR: Well, the same does apply to C99, and a C99-compiler is a requirement for POSIX now, so really it's just Microsoft that's sitting with its thumb up somewhere.
@PaulR: Oh, I'm certainly not advocating against treating int main() like any other function, including explicitly returning a proper value. That's what I do, myself.
|
2

Not in a loop, but with the predefined __COUNTER__ the following can be done.

Construct a macro defining the declaration and the function call with a counter parameter (CALLFUNC0), so counter can be used twice without incrementing it.

Then build a chain of macros calling the previous macro twice (CALLFUNC2, CALLFUNC4, ...) For 30 functions until CALLFUNC16 is needed (16+8+4+2=30).

Then define a function using the CALLFUNC30 macro. The CALLFUNC30 macro can be used only once because __COUNTER__ is incremented every time it is used.

In my example the functions names are void myfuncx1(int, int) to void myfuncx30(int, int)

/* callmyfunctions.h */

#define PASTE0(x,y) x ## y
#define PASTE1(x,y) PASTE0(x,y)

extern void callmyfuncs(int arg1, int arg2);

/* callmyfunctions.c */

#include "callmyfunctions.h"

_Static_assert(__COUNTER__ == 0, "__COUNTER__ must not be used before");
#define CALLFUNC0(func, counter, ...) extern void PASTE1(func,counter)(int arg1, int arg2);\
                                      PASTE1(func,counter)(__VA_ARGS__);
#define CALLFUNC1(func, ...) CALLFUNC0(func, __COUNTER__, __VA_ARGS__)
#define CALLFUNC2(func, ...) CALLFUNC1(func, __VA_ARGS__) CALLFUNC1(func, __VA_ARGS__)
#define CALLFUNC4(func, ...) CALLFUNC2(func, __VA_ARGS__) CALLFUNC2(func, __VA_ARGS__)
#define CALLFUNC8(func, ...) CALLFUNC4(func, __VA_ARGS__) CALLFUNC4(func, __VA_ARGS__)
#define CALLFUNC16(func, ...) CALLFUNC8(func, __VA_ARGS__) CALLFUNC8(func, __VA_ARGS__)

#define CALLFUNC30(func, ...) CALLFUNC16(func, __VA_ARGS__) CALLFUNC8(func, __VA_ARGS__) \
                              CALLFUNC4(func, __VA_ARGS__) CALLFUNC2(func, __VA_ARGS__)

void callmyfuncs(int arg1, int arg2)
{
    CALLFUNC30(myfuncx, arg1, arg2)
}

    /* callmyfunctions_test.c */

    #include <stdio.h>

    #include "callmyfunctions.h"

    _Static_assert(__COUNTER__ == 0, "__COUNTER__ must not be used before");
    #define DEFINEFUNC1(func) void PASTE1(func,__COUNTER__)(int arg1, int arg2) {\
            printf("%s %d %d\n", __func__, arg1, arg2); \
        }
    #define DEFINEFUNC2(func) DEFINEFUNC1(func) DEFINEFUNC1(func)
    #define DEFINEFUNC4(func) DEFINEFUNC2(func) DEFINEFUNC2(func)
    #define DEFINEFUNC8(func) DEFINEFUNC4(func) DEFINEFUNC4(func)
    #define DEFINEFUNC16(func) DEFINEFUNC8(func) DEFINEFUNC8(func)
    #define DEFINEFUNC30(func) DEFINEFUNC16(func) DEFINEFUNC8(func) \
                               DEFINEFUNC4(func) DEFINEFUNC2(func)

    DEFINEFUNC30(myfuncx)

    int main(void) {
        callmyfuncs(0,1);
        callmyfuncs(2,3);
        return 0;
    }

$ gcc callmyfunctions.c callmyfunctions_test.c -Wall -Wextra

$ ./a.out
myfuncx1 0 1
myfuncx2 0 1
myfuncx3 0 1
myfuncx4 0 1
myfuncx5 0 1
myfuncx6 0 1
myfuncx7 0 1
myfuncx8 0 1
myfuncx9 0 1
myfuncx10 0 1
myfuncx11 0 1
myfuncx12 0 1
myfuncx13 0 1
myfuncx14 0 1
myfuncx15 0 1
myfuncx16 0 1
myfuncx17 0 1
myfuncx18 0 1
myfuncx19 0 1
myfuncx20 0 1
myfuncx21 0 1
myfuncx22 0 1
myfuncx23 0 1
myfuncx24 0 1
myfuncx25 0 1
myfuncx26 0 1
myfuncx27 0 1
myfuncx28 0 1
myfuncx29 0 1
myfuncx30 0 1
myfuncx1 2 3
myfuncx2 2 3
myfuncx3 2 3
myfuncx4 2 3
myfuncx5 2 3
myfuncx6 2 3
myfuncx7 2 3
myfuncx8 2 3
myfuncx9 2 3
myfuncx10 2 3
myfuncx11 2 3
myfuncx12 2 3
myfuncx13 2 3
myfuncx14 2 3
myfuncx15 2 3
myfuncx16 2 3
myfuncx17 2 3
myfuncx18 2 3
myfuncx19 2 3
myfuncx20 2 3
myfuncx21 2 3
myfuncx22 2 3
myfuncx23 2 3
myfuncx24 2 3
myfuncx25 2 3
myfuncx26 2 3
myfuncx27 2 3
myfuncx28 2 3
myfuncx29 2 3
myfuncx30 2 3

Comments

1
 #include <stdio.h>
 #define MAX 5
 void function1();
 void function2();
 void function3();
 int main()
 {
    int i;
    for(i=1;i<MAX;i++)
    {
        switch(i)
        {
           case 1: function1(); break;
           case 2: function2(); break;
           case 3: function3(); break;
        }  
    }
    return 0;
 }
 void function1()
 {
   printf("In One");
 }
 void function2()
 {
   printf("In Two");
 }
 void function3()
 {
   printf("In Three");
 }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.