Search code examples
cfunctionfor-loopfunction-call

how to access 6 different functions within for loop in C?


I am writing a code in C. I have to calculate some coefficients named as:

k1, k2, k3, k4

I have 6 different functions named as:

func1, func2, func3, .....func6

Very inefficient way is to write a code like this:

/* find k1 for all 6 functions */

/* k[0][0] is k1 for func1 */
/* k[0][1] is k1 for func2 */
   ......
   ......
/* k[0][5] is k1 for func6 */

/* h is some constant */

            k[0][0] = h*func1()
            k[0][1] = h*func2()
            k[0][2] = h*func3()
            k[0][3] = h*func4()
            k[0][4] = h*func5()
            k[0][5] = h*func6()

Similarly I have to find out k2 for all 6 functions.

Again very inefficient way would be :

        k[1][0] = h*func1()
        k[1][1] = h*func2()
        k[1][2] = h*func3()
        k[1][3] = h*func4()
        k[1][4] = h*func5()
        k[1][5] = h*func6()

And similar things for remaining k3 & k4 would be:

 /* find K3 for all 6 functions */
   .............
   .............
 /* find K4 for all 6 functions */
   .............
   .............

I want to avoid all this.

I want a way so that I can call 6 functions for each coefficient k within a for loop. Something like this:

for(i=0; i<=3; i++)
  {
     for(j=0; j<=5; j++)
       {
          k[i][j] =  h*func[...]
          /* where func[...] means some way for calling 6 functions */
       }
  }

May be some array of functions ??

Any help will be highly appreciated.

Thanks.


Solution

  • You can declare an array of pointers to functions of the same type. Some people prefer to use typedefs for this, but you can also declare the array directly. If f is a pointer to a function, you can call the function with (*f)(...), where the ellipsis represents the arguments. But you can also call the function with f(...).

    #include <stdio.h>
    
    double f1(int);
    double f2(int);
    double f3(int);
    double f4(int);
    double f5(int);
    double f6(int);
    
    int main(void)
    {
        /* With typedef */
    //    typedef double (*Fn_ptr)(int);
    //    Fn_ptr funcs[6] = { f1, f2, f3, f4, f5, f6 };
    
        /* Without typedef */
        double (*funcs[6])(int) = { f1, f2, f3, f4, f5, f6 };
    
        double k[4][6];
        double h = 0.1;
    
        for (size_t i = 0; i < 4; i++)
        {
            for (size_t j = 0; j < 6; j++)
            {
                k[i][j] =  h*funcs[j](i);
            }
        }
    
        for (size_t i = 0; i < 4; i++)
        {
            for (size_t j = 0; j < 6; j++)
            {
                printf("%10f", k[i][j]);
            }
            putchar('\n');
        }
    
        return 0;
    }
    
    double f1(int x)
    {
        return x + 1;
    }
    
    double f2(int x)
    {
    
        return x + 2;
    }
    
    double f3(int x)
    {
        return x + 3;
    }
    
    double f4(int x)
    {
        return x + 4;
    }
    
    double f5(int x)
    {
        return x + 5;
    }
    
    double f6(int x)
    {
        return x + 6;
    }