user9806643
user9806643

Reputation:

Constructor won't take array initialized with new, as a parameter

Suppose i have created a constructor that takes an int m[5][5].Whenever i initialize an array in main like:(int k[5][5];) and pass it as an argument to the constructor it works fine.Yet,i have tried allocating the 2-d array as below:

  int **d=new int*[5];
   for(int i=0;i<5;i++){
   d[i]=new int[5];  }

    //5x5 matrix

and the constructor won't take the array as a parameter. Why is this happening?

Upvotes: 0

Views: 36

Answers (2)

int d[5][5];

does not define a double pointer although the syntax may lead you to think so. See Why can't we use double pointer to represent two dimensional arrays?

Upvotes: 1

Khaled Mohamed
Khaled Mohamed

Reputation: 406

A pointer to pointers each carrying an array is not a 2D array, so d[n][m] is not **d, although you may be treating both with the same way to get values from them as follows: d[i][j].

So either make your constructor as follows:

className(int **d);

or just pass a normal 2D array and your constructor will be like this:

className(int d[5][5]);

Upvotes: 0

Related Questions