title: JNA (Java): How to pass an array of pointers to struct?
body: <p>I'm using JNA to call into a C library from Java.</p>
  
  <p>In my C code I have:</p>
  
  <pre><code>   void printStructArray( SomeStruct **someStruct, int arraySize );
  </code></pre>
  
  <p>This expects an array of pointers to struct, ie the method does this:</p>
  
  <pre><code>void printStructArray( SomeStruct **someStruct, int arraySize ) {
     for( int i = 0; i &lt; arraySize; i++ ) {
        cout &lt;&lt; "someStruct: " &lt;&lt; someStruct[i]-&gt;aLong &lt;&lt; " " &lt;&lt; someStruct[i]-&gt;aString &lt;&lt; " " &lt;&lt; someStruct[i]-&gt;aDouble &lt;&lt; endl;
     }
  }
  </code></pre>
  
  <p>It's just a toy example, but I have an actual library I want to talk to which needs the same type of argument, but I think using a toy example is easier to explain here?</p>
  
  <p>I've tried all sorts of things, but I'm not sure how to (i) declare this function in JNA (ii) call this function in JNA.</p>
  
  <p>My latest (failed) attempt is:</p>
  
  <pre><code>SomeStruct.byReference[] structs = new SomeStruct.byReference[]{
          new SomeStruct.byReference(123,"hey!",1.23),
          new SomeStruct.byReference(456,"cool!",1.45),
          new SomeStruct.byReference(789,"world!",1.67) };
  PointerByReference pointerByReference = new PointerByReference(structs[0].getPointer());
  JniTest.instance.printStructArray(pointerByReference, 3);
  </code></pre>
  
  <p>This causes a SIGSEGV.</p>
  
  <p>Alternatively:</p>
  
  <pre><code>SomeStruct.byReference[] structs = (SomeStruct.byReference[]) new SomeStruct().toArray( new SomeStruct.byReference[]{
          new SomeStruct.byReference(123,"hey!",1.23),
          new SomeStruct.byReference(456,"cool!",1.45),
          new SomeStruct.byReference(789,"world!",1.67) } );
  PointerByReference pointerByReference = new PointerByReference(structs[0].getPointer());
  JniTest.instance.printStructArray(pointerByReference, 3);
  </code></pre>
  
  <p>This causes an ArrayStoreException</p>
  
  <p>Tried this also:</p>
  
  <pre><code>SomeStruct.byReference[] structs = new SomeStruct.byReference[]{
      new SomeStruct.byReference(123,"hey!",1.23),
      new SomeStruct.byReference(456,"cool!",1.45),
      new SomeStruct.byReference(789,"world!",1.67) };    JniTest.instance.printStructArray(structs, 3);
  </code></pre>
  
  <p>With method declared as:</p>
  
  <pre><code>void printStructArray(SomeStruct.byReference[] someStructarray, int num);
  </code></pre>
  
  <p>This gives '0' as the output from the function, although the good point is it doesn't crash, but it's not giving correct behavior either.</p>
  
  <p>Thoughts?</p>