wanderer - 1 month ago 5x

C Question

I am trying to define a function pointer which calculates e^-x.

Something similar to C# equivalent:

`Func<double, double> f = x => Math.Exp(-x);`

I tried in vain by doing something like:

`double(*negativeExp(double x))(double) {`

double eValue = exp(1);

return pow(eValue, -x);

}

Any ideas.

Answer

The code for the function would be:

```
double f(double x)
{
return exp(-x);
}
```

Then you can make a pointer to that function. Sample use:

```
int main(void)
{
double (*p)(double) = &f;
printf("f(1) == %f", p(1));
}
```

Source (Stackoverflow)

Comments