C language. There are functions like:

void func_0() {} void func_1() {} ... void func_n() {} 

Is it possible to somehow create a loop to call these functions so as not to call each one manually?

 for (int i = 0; i < n; i++) func_(i)(); // вызов i-той функции 

You can create an array of pointers to these functions, but again, how to fill this array automatically, without having to manually enter each pointer there?

  • And what is meant by automatic filling? How about you see it? - freim pm
  • ̀ ̀ ̀ void (* options [4]) (void) = {func_0, func_1, func_2, func_3}; for (int i = 0; i <4; i ++) (* options [i]) (); // call the i-th function ̀ ̀ ̀ How to fill an array, not manually, but using a loop? For example, if these functions will be 100 pieces. - Kirill Somov

2 answers 2

There is no normal way to automate the filling of an array.

The best option is to do something different.

You can use one function:

 void func_i(int i) { switch (i) {...} } 

If you wrote in C ++, you can use lambda functions and place them immediately in an array of function pointers:

 void (*array[])() = { [](){...}, [](){...}, [](){...}, }; 

Backup option:

It is possible to “automate” the process of filling an array with the help of serious macro-magic, but the end result will still be like that. - (c) AnT

Here it is this magic, straight from the (disgusting) Boost.Preprocessor.

 #include <stdio.h> #include <boost/preprocessor/cat.hpp> #include <boost/preprocessor/arithmetic/inc.hpp> #include <boost/preprocessor/comparison/not_equal.hpp> #include <boost/preprocessor/repetition/for.hpp> #include <boost/preprocessor/tuple/elem.hpp> #define MACRO_FOR_PRED(r, state) BOOST_PP_NOT_EQUAL( BOOST_PP_TUPLE_ELEM(3, 0, state), BOOST_PP_INC(BOOST_PP_TUPLE_ELEM(3, 1, state)) ) #define MACRO_FOR_OP(r, state) ( BOOST_PP_INC(BOOST_PP_TUPLE_ELEM(3, 0, state)), BOOST_PP_TUPLE_ELEM(3, 1, state), BOOST_PP_TUPLE_ELEM(3, 2, state) ) #define MACRO_FOR_MACRO(r, state) BOOST_PP_TUPLE_ELEM(3, 2, state) (BOOST_PP_TUPLE_ELEM(3, 0, state)) #define MACRO_FOR(from, to, macro) BOOST_PP_FOR((from, to, macro), MACRO_FOR_PRED, MACRO_FOR_OP, MACRO_FOR_MACRO) void func_0() {puts("func_0");} void func_1() {puts("func_1");} void func_2() {puts("func_2");} int main() { #define LOOP_BODY(x) BOOST_PP_CAT(func_,x), void (*array[])(void) = { MACRO_FOR(0, 2, LOOP_BODY) }; // 0 и 2 - номера первой и последней функции. #undef LOOP_BODY for (int i = 0; i < (sizeof array / sizeof array[0]); i++) array[i](); } 

Run the code

(Redid the example from here .)

Minuses:

  • The number of functions is limited by the number of BOOST_PP_LIMIT_MAG (256 by default; can be changed by regenerating library headers).

  • The numbers of the first and last functions in the array must be known at the compilation stage (preprocessing). They cannot even be placed in constant variables, only in macros.

  • Some slower compilation.

  • one
    The question seems to be about C, and you have examples on the pros. - 0xdb 4:38 pm
  • one
    @ 0xdb Exactly, did not notice. corrected. - HolyBlackCat pm

Is it "manual" or not "manual"?

 void (*const functions[N])() = { func_0, func_1, ... func_n }; for (unsigned i = 0; i < N; ++i) functions[i](); 

It is possible to “automate” the process of filling an array with the help of serious macro-magic, but the end result will still be like that.

PS In C, however, it is better (void) , but not () .

  • And what if there are 100 of such functions: do you manually write all 100 of them into an array? - Kirill Somov pm
  • @KirillSomov And why can you need 100 functions? What specific problem are you trying to solve? It will not be possible to replace all this with one function, inside of which there will be a switch from 0 to 99 ? - HolyBlackCat pm
  • In my case of functions 10. I implemented it in the same way, but then a purely theoretical question arose whether it could be “automated”. - Kirill Somov 4:23 pm