title: PInvoke, function call with pointer-to-pointer-to-pointer parameter
body: <p>I am creating a new question here as I now know how to ask this question, but I'm still a newb in PInvoke.</p>
  
  <p>I have a C API, with the following structures in it:</p>
  
  <pre><code>    typedef union pu
      {
          struct  dpos   d;
          struct  epo    e;
          struct  bpos   b;
          struct  spos   c;
       } PosT ;
  
  
      typedef struct dpos
      {
          int     id;                       
          char    id2[6];      
          int     num;
          char    code[10];
          char    type[3];
       } DPosT ;
  </code></pre>
  
  <p>and the following API function:</p>
  
  <pre><code>    int addPos(..., PosT ***posArray,...)
  </code></pre>
  
  <p>the way I call this in C like this:</p>
  
  <pre><code>    int main(int argc, const char *argv[])
      {
          ...
          PosT  **posArray = NULL;
          ...
  
          ret_sts = addPos(..., &amp;posArray, ...);
          ...
      }
  </code></pre>
  
  <p>inside addPos() memory will be allocated to posArray and it will also be populated. allocation is like this using calloc:</p>
  
  <pre><code>    int addPos(..., PosT ***posArray, ...)
      {
           PosT **ptr;
           ...
           *posArray = (PosT **) calloc(size, sizeof(PosT *));
           *ptr = (PosT *)calloc(...);
           ...
           (*posArray)[(*cntr)++] = *ptr;
           ...
  
           /* Population */
           ...
        }
  </code></pre>
  
  <p>I have another function that will be called to deallocate that memory.</p>
  
  <p>Now I want to do the same in C#, </p>
  
  <p>I have created this in my C# class:</p>
  
  <pre><code>    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
      public struct DPositionT
      {
         public int Id;
  
         [MarshalAs(UnmanagedType.LPStr, SizeConst = Constants.Id2Len)]
         public string Id2;
  
         public int Num;
  
         [MarshalAs(UnmanagedType.LPStr, SizeConst = Constants.CodeLen)]
         public string Code;
  
         [MarshalAs(UnmanagedType.LPStr, SizeConst = Constants.TypeLen)]
         public string type;
      }
  
      [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Explicit )]
      struct PosT
      {
          [System.Runtime.InteropServices.FieldOffset(0)]
          DPosT d;
      };
  </code></pre>
  
  <p>I have only defined d, as I am only going to use this member of the union in my client code.</p>
  
  <p>Now in order to call addPos() how should I create and pass posArray ?</p>
  
  <p>your help is very much appreciated.</p>