Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer to function with some, but not all arguments fixed

I want to use Brents method as present in the Numerical Recepies Book to minimize a function. The signature of the minimzation routine essentially looks like this:

float brent(float (*f)(float), float *xmin, "other parameters not relevant to question")

As you can guess brent returns the minimum value of f and stores its argument in xmin. However, the exact form of the function I want to minimize depends on additional parameters. Say

float minimize_me(float x, float a, float b)

Once I decided on the values of a and b I want to minimize it with respect to x.

I could simply add additional arguments to all functions called, all the way down to brent, thus changing its signature to

float brent(float (*f)(float,float,float),float a ,float b , float *xmin, ...)

and consequently call (*f)(x,a,b) inside brent every time. This, however, seems not really elegant to me since I would now have to pass not only the pointer to minimize_me, but also two additional parameters down a whole chain of functions.

I suspect there could be a more elegant solution, like creating a pointer to a version of the function with a and b as fixed values.

Even if it is a really obscure solution, please don't keep it from me, since I feel it could improve my overall understanding of the language.

like image 970
cmmnn Avatar asked Feb 16 '18 13:02

cmmnn


People also ask

How do you pass pointer variables as function arguments?

Pass-by-pointer means to pass a pointer argument in the calling function to the corresponding formal parameter of the called function. The called function can modify the value of the variable to which the pointer argument points. When you use pass-by-pointer, a copy of the pointer is passed to the function.

Why is it sometimes desirable to pass a pointer to a function as an argument?

If you pass argument to a function, you can change it inside the function. Once the execution of that function has finished, the passed in variable will rename unchanged. Pass by reference or pass by pointer means that you want to keep the change once the execution of function has finished. Save this answer.

Do function pointers need to be freed?

No. You must not because free(ptr) is used only when pointer ptr is previously returned by any of malloc family functions. Passing free a pointer to any other object (like a variable or array element) causes undefined behaviour.

Can a pointer be used as a function argument?

6) Like normal data pointers, a function pointer can be passed as an argument and can also be returned from a function. For example, consider the following C program where wrapper() receives a void fun() as parameter and calls the passed function.


1 Answers

A viable way of achieving this is to use a struct to store all the values and pass a pointer to that to the function.

struct parameters{
    int valueA;
    int valueB;
};

int function(void* params){
    parameters* data = (parameters*) params;
    return data->valueA + data->valueB; // just an example
}

int main(){
    parameters myData;
    myData.valueA = 4;
    myData.valueB = 2;
    function(&myData);
    return 0;
}
like image 60
Nefrin Avatar answered Oct 26 '22 15:10

Nefrin