0

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?

  • 1
    "Why is this happening?" have you tried to read what compiler tells you? Anyway please edit your question to contain [mcve] – Slava May 17 '18 at 14:21
  • Note the existence of [`std::array`](http://en.cppreference.com/w/cpp/container/array). I.e. `std::array, 5>`. – JHBonarius May 17 '18 at 14:32

2 Answers2

1
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?

0

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]);
Khaled Mohamed
  • 406
  • 5
  • 10