This is the mail archive of the
gsl-discuss@sources.redhat.com
mailing list for the GSL project.
Re: Parameter vectors declared as const in minimized functions
Hi,
> > I have a question about (multidimensional) minimization functions; why
> > does a first argument to the function have to be a const gsl_vector *
> > type? I understand one should not attempt to change the contents of
> > this vector during unconstrained minimization, since that's
> > minimizer's job, but doesn't it unnecessarily restrict the method?
>
> I hope I didn't understand what you are trying to say ...
>
> In the c programming language, a 'const' function parameter just means
> that the subroutine promises not to alter the value of that parameter.
> It does not mean that the caller needs to also declare it as a const.
But of course. I'm not trying to ask whether it's legal to pass a value to
the function taking const, I'm trying to say that in particular cases
(e.g. when minimizing the cost function instead of any analytic function)
you may want to change the argument to that function. Consider the
following snippet:
/* Not actually compiled and tested */
int f (const int *arg)
{
if (*arg > 5) *arg -= 5;
return *arg+2;
}
int main ()
{
int var = 10;
printf ("Result: %d\n", f(&var));
return 0;
}
Now obviously you pass to f() whatever (int *), but the if statement in
the f() function cannot be done, because arg is read-only, as is assured
by the keyword const.
Best wishes,
Andrej