Sujeet
Sujeet

Reputation: 3810

cannot convert 'double (*)[4]' to 'double**' in assignment

I was following a tutorial here. I came across the following lines of code.

double** pvalue  = NULL;      // Pointer initialized with null 
pvalue  = new double [3][4];  // Allocate memory for a 3x4 array 

When I compiled it, it is throwing an error

"cannot convert 'double (*)[4]' to 'double**' in assignment"

Is the code invalid? or I'm doing something wrong.Also, please, if possible, describe how could I declare pointer to multi dimension array?

Upvotes: 1

Views: 4825

Answers (5)

AnT stands with Russia
AnT stands with Russia

Reputation: 320541

As the compiler explicitly told you, new double [3][4] evaluates to a pointer of double (*)[4] type

double (*pvalue)[4] = new double [3][4];

You can't store that value in a double ** pointer.

P.S. The "tutorial" you linked is just nonsensical garbage.

Upvotes: 0

Alt Eisen
Alt Eisen

Reputation: 648

If you want to keep allocation of the two dimensional array,

  new double [3][4]

there are two way to refer to this memory chunk, 1.,

  double* pvalue  = NULL;
  pvalue  = (double*)new double [3][4];

and 2.,

  double (* parray)[4] = NULL;
  parray = new double [3][4];

if you want to keep the pointer-to-pointer variable, i.e., double** ppvalue, then,

  double** ppvalue  = NULL;
  ppvalue = new double*[3];     (a)
  for(int i =0; i<3; i++)
      ppvalue[i]=new double[4]; (b)

basically, you need to allocate memory for the array of pointers(a), and populate those pointers with concrete addresses.

The code you posted

double** pvalue  = NULL;      // Pointer initialized with null 
pvalue  = new double [3][4];  // Allocate memory for a 3x4 array 

does not work because there is no way it can deduct the offset of pvalue, though it looks very nice and clear.

I also paste the full testing code so you can have a play:

#include <stdio.h>

int main() {
  double* pvalue  = NULL;
  pvalue  = (double*)new double [3][4];

  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 4; j++) {
      *(pvalue+i*4+j) = (double)i*(double)j;
      printf("%f\n", *(pvalue+i*4+j));
    }
  }

  double (* parray)[4] = NULL;
  parray = new double [3][4];

  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 4; j++) {
      parray[i][j] = (double)i*(double)j;
      printf("%f\n", parray[i][j]);
    }
  }

  double** ppvalue  = NULL;
  ppvalue = new double*[3];
  for(int i =0; i<3; i++)
      ppvalue[i]=new double[4];

  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 4; j++) {
      ppvalue[i][j] = (double)i*(double)j;
      printf("%f\n", ppvalue[i][j]);
    }
  }
}

Upvotes: 0

3CxEZiVlQ
3CxEZiVlQ

Reputation: 38539

You can not do it. double [3][4]; allocates a flat memory of 3 arrays double[4]. The tutorial has the error.

using d4 = double [4];
// or typedef double d4[4];
d4* pvalue  = nullptr;
pvalue  = new double [3][4];
delete[] pvalue;

Upvotes: 3

con ko
con ko

Reputation: 1406

You can't do this. If you must allocate memory by yourself, try:

double** pt=nullptr;
pt=new double[3];
for(std::size_t i=0;i<3;++i)
    pt[i]=new double[4];

the deletion is a reverse process of allocation.

And one thing you need to notice is that you shouldn't use range-based for non-member begin and end function or sizeof operator since pt is not an array.

Upvotes: 0

Achal
Achal

Reputation: 11921

This pvalue = new double [3][4]; is not correct. Find how to allocate memory for 2D array using new. May be you want like below.

int main(void) {
        double** pvalue  = NULL;        
        /* first allocate for pvalue */  
        pvalue  = new double *[3];  
        for(int col = 0; col < 3 ; col++ )
                /* allocate for each palue element */
                pvalue[col] = new double [4];
        return 0;
}

Once done, don't forget to free the dynamically allocated memory by calling delete.

Upvotes: 1

Related Questions