C Call Loop Function

Say I have functions called Function1, Function2, Function3, etc. Is there a way to call one of the functions in a loop every time?

for(i=1;i<Max;i++) { Function^(); } 
+4
source share
3 answers

Try the following:

 #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; } 
+8
source

Not in a loop, but with the predefined __COUNTER__ you can do the following.

Build a macro that defines the declaration and function call using the counter parameter ( CALLFUNC0 ), so the counter can be used twice without increasing it.

Then create a macro chain that calls the previous macro twice ( CALLFUNC2 , CALLFUNC4 , ...) For 30 functions up to CALLFUNC16 (16 + 8 + 4 + 2 = 30).

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

In my example, the function names void myfuncx1(int, int) are 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 
+2
source
  #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"); } 
+1
source

All Articles