2

I'm totally new to C. Here's the question:

Write the function

fzero(double f(double),double x1, double x2)

as we did in class and use it to find all the solutions of

sin( pi*x / (1+x^2) ) = 0.25.

Now, I don't want you to solve the this. I'd missed this lecture and only want to understand what means

double f(double);

1 Answer 1

9

In that context, it means that f is a function pointer to a function to that takes one double argument, and returns a double.

As an example:

void foo(double f(double))
{
    double y = f(3.0);  // Call function through function pointer
    printf("Output = %f\n", y);   // Prints "Output = 9.0000"
}

double square(double x)
{
    return x*x;
}

int main(void)
{
    foo(&square);  // Pass the address of square()
}

Note that there are two syntaxes for function pointers:

void foo(double f(double))
void foo(double (*f)(double))

These are equivalent.

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.