简体   繁体   中英

Alternative to nested functions in C?

What is the standard alternative to nested functions in C? How do I give one function the scope of another function? I don't really want to pass around extra parameters like a struct, use variable length input, or use the gcc extension unless there is no other way.

For my specific problem, I have this function:

double findroot(double (*fp)(double), double start, double end);

It finds the root of a function, f(x), of one variable with a function pointer as an argument.

I want to pass in a function of multiple variables where all but one are fixed. For example, f(x,a,b,c), where a, b, and c are all constants that are passed in and x is the only variable that changes. How do I find f(0,a,b,c) while keeping the findroot function the same so that the findroot function is reusable and generic and not specific to this one case?

Thanks.

If you need to find f(x,a,b,c) where a, b and c are invariant, then that suggests a new more specialised function f2(x) = f(x,a,b,c). For example:

double f2( double x )
{    
    return f( x, 1.0, 2.0, 3.0 ) ;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM