zenmonkey
zenmonkey

Reputation: 13

Initializing function pointer to new function in C

I would like to convert

typedef double (*Function)(double s[4]);
double f1 (double x[N]) {return x[0]*x[1];}
Function t = f1;

to something like this:

typedef double (*Function)(double s[4]);
Function t = ({ return x[0]*x[1];});

which returns a "void value not ignored as it ought to be" error. How can I make this work?

Upvotes: 0

Views: 54

Answers (1)

0___________
0___________

Reputation: 67476

C does not have anonymous functions or lambda expressions. So it is not possible.


EDIT: PSEUDO LAMBDA

Tiny dirty workaround for the compilers which allow nested functions:

#define lmb(rt, fb)({rt fn__fn__fn_ fb fn__fn__fn_; })

int main(void)
{
    double (*f)(double x[]) = lmb(double, (double x[]) { return x[0] * x[1]; });
    double  x[] = {4.0, 5.0};

    printf("%f\n", f(x));
}

Upvotes: 2

Related Questions