Search code examples
c++classmultidimensional-arrayconstructornew-operator

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?


Solution

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