0

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
4
  • I completely failed to see the problem. Did it compile and run? Commented Aug 19, 2014 at 9:53
  • The function provided above with arrays work, but say we declare as double* alpha; double* theta; and load values to these arrays dynamically, I am not able to pass these pointers as arguments to the function gsl_ran_dirichlet. Commented Aug 19, 2014 at 9:56
  • Yes, I am sure you can. If it works with alpha[2], it will also work for dynamically allocated alpha. In fact, alpha[2] "decays" to a pointer when you pass it to the function. Commented Aug 19, 2014 at 9:59
  • Well, I have now added the error I get to the question with the changes on the test function as well. Commented Aug 19, 2014 at 10:08

6 Answers 6

2

General:

  • Variable to pointer: &variable.
  • Pointer to variable: *pointer.

Specific:

The name of an array and a pointer to an array can be used in the same way, i.e. theta[0] and pointer_to_theta[0] are equivalent.

int foo[2] = { 1, 2 };

int * pointer_to_foo = foo;

assert( foo[1] == pointer_to_foo[1] );
Sign up to request clarification or add additional context in comments.

Comments

2

Your problem is not about calling a function.

it is simply that your

for(int i=0; i<3; ++i){
    alpha = 1;
    theta = 1;
}

is wrong.

alpha is a double* which you cannot assign a int (1) to it.

What you are trying to do is

alpha[i] = 1;

or

*(alpha + i) = 1

And! please learn the read the error message. There is a line number in the error message and it is pointing you to where the problem is happening. You should be able to find it by yourself if you look into your line 56 and 57

Comments

1

Try changing the assignments in your for loop into

alpha[i] = 1;
theta[i] = 1;

Comments

0
double* alpha;
double* tetha;

void foo()
{
   double (&refToAlpha)[2] = reinterpret_cast<double(&)[2]> (alpha);
   double (&refToTetha)[2] = reinterpret_cast<double(&)[2]> (tetha);
   ...
   gsl_ran_dirichlet(r, 2, refToAlpha, refToTetha);
}

Comments

0

The compilation error is simple: you assign an int to a pointer-to-a-double.

for( int i=0; i < 3; ++i ) {
   alpha[i] = i; // dereference before assignment
}

The 'how-to-pass-an-array' to a function is somewhat more complicated. It's common for legacy and C-compatible code to pass in the pointer to the array, together with it's size (foo( int* values, size_t size)).

If you have the freedom of choice, you would prefer the use of standard collections (i.e. a std::vector<double>) (and algorithms like iota):

std::vector<double> alpha, theta;
std::iota(begin(alpha), end(alpha));
std::iota(begin(theta), end(theta));

and pass the collections by const reference if you want the function to read them, by value if you want the function to own a copy, by reference if you want the function to change the argument (i.e. an output argument).

void gsl_ran_dirichlet(
       std::vector<double> &r, // though I prefer output args to come last
       int i, 
       const std::vector<double> &alpha, 
       const std::vector<double> &theta);

Comments

0

The Problem in your code has nothing to do with passing an Array to a function.

In your For-loop you try ]to set a Pointer (double*) to an Integer (1) which is the cause of you compiling error.

You have to Address the position of your Array with [] to set the value.

for(int i=0; i<3; ++i){
    alpha[i] = 1.0;
    theta[i] = 1.0;
}

This is identical with normal Pointers. To set the Value of the Pointer you have to dereference the Adress.

int* x = new int();
*x = 5;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.