title: Marshal a C struct containing a variable length array
body: <p>I would like to marshal a C struct with a variable-length array back to C# but so far I can't get anything better than a pointer-to-struct representation and a pointer to float.</p>
  
  <p>Unmanaged representation:</p>
  
  <pre><code>typedef float        smpl_t;
  
  typedef struct {
    uint_t length;  /**&lt; length of buffer */
    smpl_t *data;   /**&lt; data vector of length ::fvec_t.length */
  } fvec_t;
  </code></pre>
  
  <p>Managed representation: </p>
  
  <pre><code>[StructLayout(LayoutKind.Sequential)]
  public unsafe struct fvec_t1
  {
      public uint length;
  
      public float* data;
  }
  
  [DllImport("libaubio-4.dll", EntryPoint = "new_fvec", PreserveSig = true, CharSet = CharSet.Ansi,
      CallingConvention = CallingConvention.Cdecl)]
  public static extern unsafe fvec_t1* new_fvec1(uint length);
  </code></pre>
  
  <p>What I would like is to have a .NET style array, where <code>data</code> would be <code>float[]</code> but if I do change the struct to the form below I do get <em>Cannot take the address of, get the size of, or declare a pointer to a managed type</em>  in the external function above.</p>
  
  <pre><code>[StructLayout(LayoutKind.Sequential)]
  public unsafe struct fvec_t1
  {
      public uint length;
  
      public float[] data;
  }
  </code></pre>
  
  <p><a href="https://stackoverflow.com/a/5902264/361899">Apparently</a>, it is not possible to a have a variable-length array marshalled back as-is, is this correct or is it there still a way to achieve this ?</p>