title: Using arrays and pointers in C# with C DLL
body: <p>I am very new to C# (just started learning in the past week).</p>
  
  <p>I have a custom DLL written in C with the following function:</p>
  
  <pre><code>DLLIMPORT void test_function (double **test)
  </code></pre>
  
  <p>What I am looking to do is have a pointer from C# for the array 'test'.</p>
  
  <p>So, if in the DLL function I have test[0] = 450.60, test[1] = 512.99 etc. I want to be able to have that available in my C# program.</p>
  
  <p>In the C# program I have something similar to:</p>
  
  <pre><code>namespace TestUtil
  {
    public class Echo
    {
      public double[] results = new double[10];
      public double[] results_cpy = new double[10];
  
  
      [DllImport("test_dll.dll", CallingConvention = CallingConvention.Cdecl)]
      static extern unsafe void test_function(ref double[] Result);
  
      public unsafe void Tell()
      {
        results[0] = 0.0;
        results[1] = 0.0;
  
        results_cpy[0] = 0.0;
        results_cpy[1] = 0.0;
  
        test_function(ref results);
        results_cpy[0] = (double)results[0] + (double)results[1] ;
      }
    }
  }
  </code></pre>
  
  <p>In the DLL's 'test_function' function I used the following:</p>
  
  <pre><code>*test[0] = 450.60;
  *test[1] = 512.99;
  </code></pre>
  
  <p>Within the DLL everything was OK (I used a message box within the DLL to check the values were being applied). Back in the C# program 'results[0]' appears to be fine and I can get values from it, but 'results[1]' gives me an index out of bounds error. I know this because if I omit '+ (double)results[1]' I receive no error. Also, if I make no attempt within the DLL to modify 'test[1]' it retains the original value from C# (in my example 0.0).</p>
  
  <p>Obviously I am not doing something right but this is the closest I have been able to get to having it work at all. Everything else I have tried fails miserably.</p>
  
  <p>Any help would be greatly appreciated.</p>