Arun Jayapal
Arun Jayapal

Reputation: 457

How to modify and pass array pointer as argument to a function which require array as argument?

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

Upvotes: 0

Views: 336

Answers (6)

pcaaron
pcaaron

Reputation: 47

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;

Upvotes: 0

xtofl
xtofl

Reputation: 41509

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);

Upvotes: 0

lkanab
lkanab

Reputation: 954

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);
}

Upvotes: 0

Adrian Shum
Adrian Shum

Reputation: 40036

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

Upvotes: 2

downhillFromHere
downhillFromHere

Reputation: 1977

Try changing the assignments in your for loop into

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

Upvotes: 1

DevSolar
DevSolar

Reputation: 70243

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] );

Upvotes: 2

Related Questions