简体   繁体   English

如何修改并将数组指针作为参数传递给需要数组作为参数的函数?

[英]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 . 例如,在以下方法中,我们使用数组alpha和theta作为参数传递给函数gsl_ran_dirichlet ,该函数计算新的theta值并更新相同的数组theta

Now, the problem is that I will not be able to initialize theta in a class as provided in the following code piece. 现在,问题在于我将无法在以下代码段中提供的类中初始化theta。 Rather I will have to use pointers to arrays theta and alpha . 相反,我将不得不使用指向数组thetaalpha的指针。 How will I pass these array pointers as argument to the method gsl_ran_dirichlet ? 我如何将这些数组指针作为参数传递给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 )? 但是完成此操作的最佳方法是什么(假设我们无法修改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

General: 一般:

  • Variable to pointer: &variable . 指针&variable&variable
  • Pointer to variable: *pointer . 指向变量的*pointer*pointer

Specific: 具体:

The name of an array and a pointer to an array can be used in the same way, ie theta[0] and pointer_to_theta[0] are equivalent. 数组的名称和指向数组的指针可以以相同的方式使用,即, theta[0]pointer_to_theta[0]是等效的。

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

int * pointer_to_foo = foo;

assert( foo[1] == pointer_to_foo[1] );

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. alphadouble* ,您不能为其分配int (1)。

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 如果您查看第56和57行,应该可以自己找到它

Try changing the assignments in your for loop into 尝试将for循环中的分配更改为

alpha[i] = 1;
theta[i] = 1;
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);
}

The compilation error is simple: you assign an int to a pointer-to-a- double . 编译错误很简单:将int分配给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) ). 遗留和C兼容代码通常将指针及其大小( foo( int* values, size_t size) )传递给数组。

If you have the freedom of choice, you would prefer the use of standard collections (ie a std::vector<double> ) (and algorithms like iota ): 如果您有选择的自由,则最好使用标准集合(即std::vector<double> )(以及类似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 (ie an output argument). 如果希望函数读取它们,则通过const引用传递这些集合如果希望函数拥有副本, 则按 传递这些集合如果希望函数更改参数(即输出参数) ,则按引用传递。

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

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. 在您的For循环中,您尝试]将Pointer(double *)设置为Integer(1),这是导致编译错误的原因。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM