1

when i am passing int matrix[3][3] int the function rotateMatrix it is working fine but when i am passing int** program is crashing..i am a newbie in pointers plz look through my code..thanks in advance

  #include<iostream>
   using namespace std;

  int** rotateMatrix(int** matrix,int m,int n){
    int** temp=new int*[m];
     for(int i=0;i<n;i++){
      temp[i]=new int[n];
    }

    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
            temp[i][j]=matrix[m-1-j][i];

        }

     } 
    return temp;
  }

     int main(){
         int matrix[3][3]={ {2,1,3},
                          {3,4,5},
                          {6,9,7}
                         };


    int** res=rotateMatrix((int**)matrix,3,3);

        for(int i=0;i<3;i++){
          for(int j=0;j<3;j++){
              cout<<res[i][j]<<" ";
           }
           cout<<endl;
       }

      return 0;
  }
asad_nitp
  • 333
  • 2
  • 8
  • You can pass adress of the first element in the static 2d array: like this int** res = rotateMatrix(&matrix[0][0], 3, 3); And change signature of function to this: int** rotateMatrix(int* matrix, int m, int n) – arturx64 Mar 28 '16 at 13:19
  • You cannot cast `int[][]` to `int*`. See [this](http://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function), [this](http://www.geeksforgeeks.org/pass-2d-array-parameter-c/), [this](http://stackoverflow.com/questions/15766382/c-casting-static-two-dimensional-double-array-to-double), [this](http://stackoverflow.com/questions/8203700/conversion-of-2d-array-to-pointer-to-pointer), [this](http://stackoverflow.com/questions/1584100/converting-multidimensional-arrays-to-pointers-in-c) and [this](http://stackoverflow.com/questions/7586702/is-2d-array-a-double-pointer). – Anmol Singh Jaggi Mar 28 '16 at 13:21

0 Answers0