title: How do I pass an array from .Net to C?
body: <p>I am trying to make use of an <a href="https://osqp.org/docs/examples/demo.html" rel="nofollow noreferrer">open source library, written in C</a>, by wrapping it and exposing it to .Net. I am not, by any stretch, a C expert.</p>
  
  <p>So far, I have managed to call the demo code, written in C, from F#. I managed to get this far by following <a href="https://www.codeproject.com/Articles/9826/How-to-create-a-DLL-library-in-C-and-then-use-it-w" rel="nofollow noreferrer">this guide</a> then filling in the blanks. I have adapted the C code to expect an int passed to it, so I can at least take in a scalar value from F# into C. No return value though.</p>
  
  <p>But working with arrays is much trickier.</p>
  
  <p>My C code is</p>
  
  <pre><code>extern "C"
  {
      __declspec(dllexport) void DisplayHelloFromDLL(int i)
      {
          //printf("Hello from DLL !\n");
          cout &lt;&lt; "You gave me ... an int: " &lt;&lt; i &lt;&lt; endl;
  
          // Load problem data
          c_float P_x[4] = { 4., 1., 1., 2., }; //covariance matrix
          c_int   P_nnz = 4; //number of non-zero elements in covar
          c_int   P_i[4] = { 0, 1, 0, 1, }; //row indices?
          c_int   P_p[3] = { 0, 2, 4}; //?
          c_float q[2] = { 1., 1., }; //linear terms
          c_float A_x[4] = { 1., 1., 1., 1., }; //constraint coefficients matrix
          c_int   A_nnz = 4; //number of non zero elements in constraints matrix
          c_int   A_i[4] = { 0, 1, 0, 2, }; //row indices?
          c_int   A_p[3] = { 0, 2, 4}; //?
          c_float l[3] = { 1., 0., 0., }; //lower bounds
          c_float u[3] = { 1., 0.7, 0.7, }; //upper bounds
          c_int n = 2; //number of variables (x)
          c_int m = 3; //number of constraints
  
  
          // Problem settings
          OSQPSettings *settings = (OSQPSettings *)c_malloc(sizeof(OSQPSettings));
  
          // Structures
          OSQPWorkspace *work; // Workspace
          OSQPData *data;      // OSQPData
  
          // Populate data
          data = (OSQPData *)c_malloc(sizeof(OSQPData));
          data-&gt;n = n;
          data-&gt;m = m;
          data-&gt;P = csc_matrix(data-&gt;n, data-&gt;n, P_nnz, P_x, P_i, P_p);
          data-&gt;q = q;
          data-&gt;A = csc_matrix(data-&gt;m, data-&gt;n, A_nnz, A_x, A_i, A_p);
          data-&gt;l = l;
          data-&gt;u = u;
  
  
          // Define Solver settings as default
          osqp_set_default_settings(settings);
  
          // Setup workspace
          work = osqp_setup(data, settings);
  
          // Solve Problem
          osqp_solve(work);
  
          // Clean workspace
          osqp_cleanup(work);
          c_free(data-&gt;A);
          c_free(data-&gt;P);
          c_free(data);
          c_free(settings);
      }
  }
  </code></pre>
  
  <p>and my C# code is</p>
  
  <pre><code>class Program
      {
          [DllImport("TestLibCpp.dll")]
          public static extern void DisplayHelloFromDLL(int i);
          static void Main(string[] args)
          {
              Console.WriteLine("This is F# program");
              DisplayHelloFromDLL(52);
          }
      }
  </code></pre>
  
  <p>To keep the focus of this question narrow: how do I make P_x a parameter, to be passed in from F#?</p>