This probably is a beginner question. Say for example, in the following method we use the arrays alpha and theta, which are passed as argument to the function gsl_ran_dirichlet, and the function computes new theta values and updates the same array theta.
Now, the problem is that I will not be able to initialize theta in a class as provided in the following code piece. Rather I will have to use pointers to arrays theta and alpha. How will I pass these array pointers as argument to the method gsl_ran_dirichlet?
I know it is not possible to pass pointer as argument to method which require array as argument. But what is the best way to accomplish this (assume we cannot modify gsl_ran_dirichlet)?
void test (){
double alpha[2] = { 1, 1};
double theta[2] = { 1, 1};
const gsl_rng_type * T;
gsl_rng * r;
gsl_rng_env_setup();
T = gsl_rng_default;
r = gsl_rng_alloc(T);
gsl_ran_dirichlet(r, 2, alpha, theta);
cout << theta[0] << "," << theta[1] << endl;
gsl_rng_free(r);
}
Result:
0.4,0.6
Now, I am also adding the function and the error I get in the following code, where the arrays are loaded dynamically:
void test() {
double *alpha, *theta;
alpha = new double[3];
theta = new double[3];
for(int i=0; i<3; ++i){
alpha = 1;
theta = 1;
}
const gsl_rng_type * T;
gsl_rng * r;
gsl_rng_env_setup();
T = gsl_rng_default;
r = gsl_rng_alloc(T);
gsl_ran_dirichlet(r, 3, alpha, theta);
cout << theta[0] << "," << theta[1] << "," << theta[2] << ":";
gsl_rng_free(r);
}
Error:
../test.cpp:56:11: error: invalid conversion from ‘int’ to ‘double*’ [-fpermissive]
../test.cpp:57:11: error: invalid conversion from ‘int’ to ‘double*’ [-fpermissive]
make: *** [test.o] Error 1
alpha[2], it will also work for dynamically allocatedalpha. In fact,alpha[2]"decays" to a pointer when you pass it to the function.