-1

I have a function which requires **double, while the pointer I defined is *double, how to send it to the function? Sample code is the following,

void eig(int n, double **A);
int main(){
  int DIM=5;
  double *HMAT = new double [DIM*DIM];
  eig(DIM,HMAT);
  delete [] HMAT;
}

void eig(int n, double **A) {}

Compilation complains

main.cc:8:15: error: cannot convert ‘double*’ to ‘double**’ for argument ‘2’ to ‘void eig(int, double**)’
bsmile
  • 59
  • 8
  • The compiler complains about `double *HMAT = new double *[DIM*DIM];` which is wrong - did you mean `new double[DIM*DIM];`? Also I guess you want to do `eig(DIM,&HMAT);` for the function call? (Hard to tell without knowing what that function is supposed to do) – UnholySheep May 04 '17 at 21:04
  • You need to post what `eig` is doing with that `double**` parameter. We don't know if its goal is to initialize that pointer within the function, or to use it like a 2d array, etc. – PaulMcKenzie May 04 '17 at 21:09
  • I am sure you don't need that pointer fiddeling and new. Use a container. – The Techel May 04 '17 at 21:09
  • Thanks for pointing out the typo, I have corrected it. The error message is the same (ok, should be relevant if an undefined pointer can get defined in a function). What eig is doing is irrelevant as compiler complains the interfacing issue. The goal is to use it like a 2D array for eigenvalue evaluation. – bsmile May 04 '17 at 21:56
  • @bsmile No it is relevant. It is not clear whether `eig` does something like this: `void eig(int n, double **A) { *A = new double[n]; ... return;}` and in that case the call would look like this: `double *p; eig(10, &p);` -- or if `eig` really uses `A**` as a two dimensional array. If it indeed uses `A` like a 2D array, then [create one from a double**](http://stackoverflow.com/questions/21943621/how-to-create-a-contiguous-2d-array-in-c/21944048#21944048) – PaulMcKenzie May 04 '17 at 22:05
  • Cannot edit my comment now, so add a new one here. The error message is different, and I have put the new error message there. Thanks for Paul's comment, I just got to know that an undefined pointer can get allocated in a function. Here HMAT has already been defined and eig basically get its eigenvalue. – bsmile May 04 '17 at 22:05
  • Who gave me negative votings? It's obviously a typo, and I will correct it to remove confusion. Also why Stephen below got a negative voting as well? His response is quite good. – bsmile May 04 '17 at 22:07
  • @bsmile unfortunately there are some trolls around... if they don't want to answer a question they'll downvote it and also downvote anyone who does answer . Voting is anonymous though so we cannot know who or why for sure unless they choose to comment – M.M May 04 '17 at 23:37

2 Answers2

0

I think the misunderstanding is that void eig(int n, double **A) does not denote a "pointer to a 2D-array" but a pointer to a pointer to a double value. Depending on what you do with A in function eig, this can be meaningful or not: If eig is about allocating or reallocating a new array, then you'd use a double **A in order to pass back the newly allocated memory to the caller. However, if you just want to access the values of the array, it's sufficient to pass just a pointer to the array (not a pointer to a pointer). See the following code explaining the difference. Hope it helps.

void eig1(int n, double **A) {
    *A = new double [n*n];  // OK; pass back the array by writing its starting address into the pointer to which A points.
}

void eig2(int n, double *A) {
    A = new double [n*n];  // does not make sense; changes local variable `A` only; will result in a memory leak
}

void eig_populate(int n, double *A) {
    for (int i=0;i<n;i++) {
        A[i]=i;  // OK; does not allocate a new array but accesses an existing one
    }
}

int main(){
    double *HMAT = nullptr;
    eig2(5,HMAT); // will not change HMAT
    eig1(5,&HMAT); // will change HMAT
    eig_populate(5, HMAT);
}
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
0

You've misread the error message. Pay attention to line numbers: the problem isn't the call to eig, it's the assignment of a double** created by new *[DIM*DIM] to HMAT, whose type is double*. To fix that error, change the type of HMAT to double**.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165