title: JNA - Use structure array as byref argument
body: <p>I know parts of this issue is covered by some posts here and I have looked at them and tested some but with no luck.
  I have this native method signature which should populate the provided <strong>CBadgeData structure array</strong> with results:</p>
  
  <pre><code>int elc_GetBadges(int nHandle, char* cErr, int* nRecCount, CBadgeData** arr)
  </code></pre>
  
  <p>The CBadgeData structure is implemented as follows:</p>
  
  <pre><code>package test.elcprog;
  
  import java.util.Arrays;
  import java.util.List;
  import com.sun.jna.Pointer;
  import com.sun.jna.Structure;
  
  public class CBadgeData extends Structure{
  
      public static class ByReference extends CBadgeData implements Structure.ByReference { }
  
      public int nBadgeID, nTrigger, nExtraData;
      public String cName;
  
      public CBadgeData(Pointer pointer){
          super(pointer);
      }
  
      public CBadgeData(){ }
  
      public String ToString() {
          return nBadgeID + "," + nTrigger + "," + nExtraData + "," + cName;
      }
  
      @Override
      protected List getFieldOrder() {
          String[] s = new String[]{"nBadgeID","nTrigger","nExtraData","cName"};
          return Arrays.asList(s);
      }
  }
  </code></pre>
  
  <p>My last try to craft this argument and call the method looked like this:<br></p>
  
  <pre><code>CBadgeData.ByReference[] badges = new CBadgeData.ByReference[max_items];
  new CBadgeData.ByReference().toArray(badges);
  int ret = inst.elc_GetBadges(handle, err, recCount, badges);
  </code></pre>
  
  <p>It fails with segmentation error.<br></p>
  
  <p>My Question is what Java type should be provided here as an argument for the native <code>CBadgeData**</code> in the call to <code>elc_GetBadges</code>?<br></p>
  
  <p><strong>EDIT -1-</strong></p>
  
  <p>Populating the array myself (with or without terminating null pointer) didn't work and caused further Seg crashes. I then used Pointer[] arg as technomage suggested:</p>
  
  <pre><code>Pointer[] pointers = new Pointer[max_items];
  for(int i=0; i&lt;max_items; i++){
      pointers[i] = new CBadgeData.ByReference().getPointer();
  }
  int ret = inst.elc_GetBadges(handle, err, recCount, pointers);
  </code></pre>
  
  <p>This caused no error but seems to not make any changes to the returning struct which should have contain 4 items in this case:</p>
  
  <pre><code>int bid = new CBadgeData(pointers[i]).nBadgeID; // this returns null for all items
  </code></pre>
  
  <p>Using explicit read() / write() on the struct led to Seg crashes again (on the read):
  Any idea what am I still missing here?</p>
  
  <p><br><strong>EDIT -2-</strong><br></p>
  
  <p>Interestingly enough - using the <code>Memory.get</code> directly, after calling the native method, gets the correct results:</p>
  
  <pre><code>Memory m= (Memory)pointers[0];
  System.out.println("1st int: "+m.getInt(0)); // this gets 24289 which is 5ee1
  System.out.println("2nd int: "+m.getInt(4)); // this gets 3
  System.out.println("3rd int: "+m.getInt(8)); // this gets 255
  System.out.println("String: "+m.getString(12)); // this gets "Badge[5EE1]" as supposed
  </code></pre>
  
  <p>But the <code>read()</code> still crashes. Any thoughts?</p>