简体   繁体   English

C函数调用循环

[英]C Function Call Loop

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? 假设我有一些名为Function1,Function2,Function3等的函数。有没有办法每次调用循环中的一个函数?

for(i=1;i<Max;i++)
{
Function^();
}

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;
}

Not in a loop, but with the predefined __COUNTER__ the following can be done. 不在循环中,但使用预定义的__COUNTER__可以完成以下操作。

Construct a macro defining the declaration and the function call with a counter parameter ( CALLFUNC0 ), so counter can be used twice without incrementing it. 使用计数器参数( CALLFUNC0 )构造一个定义声明和函数调用的宏,这样计数器可以使用两次而不增加它。

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). 然后构建一个宏链,调用前一个宏两次( CALLFUNC2CALLFUNC4 ,...)30个函数,直到需要CALLFUNC16 (16 + 8 + 4 + 2 = 30)。

Then define a function using the CALLFUNC30 macro. 然后使用CALLFUNC30宏定义一个函数。 The CALLFUNC30 macro can be used only once because __COUNTER__ is incremented every time it is used. CALLFUNC30宏只能使用一次,因为__COUNTER__每次使用时都会递增。

In my example the functions names are void myfuncx1(int, int) to void myfuncx30(int, int) 在我的例子中,函数名是void myfuncx1(int, int)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
 #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");
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM