Home > OS >  I don't need an output of a function, how to give it undeclared address to store that output
I don't need an output of a function, how to give it undeclared address to store that output

Time:04-02

I just started learning C.

I have a function with two outputs, a value and an estimated error.

double result, abserr;
gsl_deriv_central (&f_var, rho, 1e-8, &result,&abserr);

I only care about the result. So my question is, can I skip the declaration of absolute error abserr. And give as input something like:

double result;    
gsl_deriv_central (&f_var, rho, 1e-8, &result,&SOMEWHERE);

Suggests better tags, if it seems incomplete.

CodePudding user response:

You probably want something like this:

Original function:

void gsl_deriv_central(f_type *f_var, double rho, double bar, double *result, double *abserr)
{
 ...
}

f_type is whatever type used for the first parameter of the gsl_deriv_central function (which is unknown to me).

Wrapper:

void gsl_deriv_central2(f_type *f_var, double rho, double bar, double *result)
{
  double dummy;   // dummy variable, we just ignore it

  // call original function
  gsl_deriv_central(f_var, rho, bar, result, &dummy);
 ...
}

Call the wrapper function like this

double result;    
gsl_deriv_central2(&f_var, rho, 1e-8, &result);

CodePudding user response:

As pointed out, first check if the library supports this call:

int status = gsl_deriv_central (&f_var, rho, 1e-8, &result, NULL);

If it doesn't use a wrapper like :

static inline int
my_gsl_deriv_central (const gsl_function *f_var, double rho, double step, double* result) {
    double abserr;
    return gsl_deriv_central (f_var, rho, step, result, &abserr);
}

You call my_gsl_deriv_central from your translation unit.

  • Related