title: passing 2D array from C# to unmanaged C++ using C++/CLI wrapper
body: <p>I am working in a project that requires optimization and I want to translate one part of the code from C# to C++. I am using a C++\CLI wrapper but I am really new with this approach and I have not come to understand it completely. The following code returns an error when I run the program and I don't know the reason.</p>
  
  <p>The C# program is the following:</p>
  
  <pre><code>int[,] Arr = new int[5, 5];
  
          for (int i = 0; i &lt; 5; i++)
          {
              for (int j = 0; j &lt; 5; j++)
              {
                  Arr[i, j] = i + j;
              }
          }
  
  test.MatrixComputation(Arr, Arr.GetLength(0));
  </code></pre>
  
  <p>The C++/CLI project as follows:</p>
  
  <pre><code>void MatrixComputation(cli::array&lt;int, 2&gt; ^arr, int size)
      {
          pin_ptr&lt;int&gt; p_arr = &amp;arr[0, 0];
          pu -&gt; ChangeArray((int**)p_arr, size);
      }
  </code></pre>
  
  <p>The Unmanaged C++ code:</p>
  
  <pre><code>void Unmanaged::MatrixComputation(int** arr, int size)
  {
      for (int i = 0; i &lt; size; i++)
      {
          for (int j = 0; j &lt; size; j++)
          {
              arr[i][j] = i + j;     // line 27
          }
      }
  
      std::cout &lt;&lt; arr[2][2] &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>It compiles well but when I run it I get the following error:</p>
  
  <pre><code>*Line 27: System.NullReferenceException: Object reference not set to an instance of an object*
  </code></pre>
  
  <p>The casting to double pointer is a bad strategy but it is the only thing that came into my mind. Also, I know that C++ doesn't have 2D arrays just like C# has, but I need the multidimensional array in C# and I can not change it to write a jagged array Arr[][].</p>
  
  <p>Thanks in advance.</p>