Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C - Pass multiple function addresses as parameters in a variadic function

I'm trying to write a function that will take the first n integers and a variable number of functions and build a table that has the number as "i" in the first column and "function(i)" in the others.

But I cannot seem to be able to pass the addresses of my functions to the table generator because I get an access violation error. What did I do wrong?

#include <stdio.h>
#include <math.h>
#include <stdarg.h>

typedef float(*f)(float);

// Some examples of f-type functions.
float square(float x) { return x*x; };
float root(float x) { return sqrt(x); };
float timesPi(float x) { return x * 3.14; };

// Display a table with first colon being the numbers from 1 to n, 
// then the other columns to be f(i)
void table(unsigned int n, unsigned int nr_functions, ...)
{
    va_list func;
    va_start(func, nr_functions);

    for (float i = 1; i <= n; i += 1)
    {
        printf("\n%6.0f |", i);
        for (unsigned int j = 0; j < nr_functions; j++)
        {
            f foo = va_arg(func, f);
            printf("%6.3f |", foo(i));
        }
        va_end(func);
    }
}

// Main function
int main()
{
    table(5, 3, &square, &root, &timesPi);
    system("pause");
    return 0;
}

For the example above

table(5, 3, &square, &root, &timesPi);

I want to get back

1   |   1.000 |  3.140 |
2   |   1.141 |  6.280 |
3   |   1.732 |  9.420 |
4   |   2.000 | 12.560 | 
5   |   2.236 | 15.700 |
like image 529
Jadarma Avatar asked Sep 26 '22 21:09

Jadarma


1 Answers

You need to reuse the variable section of the argument list, which means you need the va_start() and va_end() in the right places — inside the outer loop:

void table(unsigned int n, unsigned int nr_functions, ...)
{
    for (unsigned int i = 1; i <= n; i++)
    {
        va_list func;
        printf("\n%6.0f |", (double)i);
        va_start(func, nr_functions);
        for (unsigned int j = 0; j < nr_functions; j++)
        {
            f foo = va_arg(func, f);
            printf("%6.3f |", foo(i));
        }
        va_end(func);
    }
}

Otherwise, you're marching off the end of the list, except that you called va_end() inside the loop, leading to goodness only knows what damage.

Note that the loop should use integer arithmetic — with a consequential change to the printf() — here I cast the value, but changing the format to %6d would also be sane (possibly better, in fact).

With this function, I got the output:

 1 | 1.000 | 1.000 | 3.140 |
 2 | 4.000 | 1.414 | 6.280 |
 3 | 9.000 | 1.732 | 9.420 |
 4 |16.000 | 2.000 |12.560 |
 5 |25.000 | 2.236 |15.700 |
like image 192
Jonathan Leffler Avatar answered Oct 11 '22 23:10

Jonathan Leffler