0

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?

2
  • C doesn't have lambdas unless this is a compiler specific extension. Commented Nov 28, 2020 at 18:51
  • @SuperStormer dirty workaround in my answer - but requires nested functions extension. Commented Nov 28, 2020 at 20:24

1 Answer 1

2

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));
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.