Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I call a C function expecting an array of structs from Java using swig?
    primarykey
    data
    text
    <p>I have a C function that takes an array of structures as an argument, and I want to call this function from Java by way of SWIG, but the documentation seems quite murky on this subject and I can't find any stackoverflow questions that directly address this case. Here is an example that is similar to what I want to do:</p> <p>C header file:</p> <pre><code>typedef struct { int timeToPayment; double paymentAmount; } Payment; double presentValue(Payment *payments, int nPayments); </code></pre> <p>Java code snippet:</p> <pre><code>... Class Payment { public int timeToPayment; public double paymentAmount; }; ... Payment[] payments = new Payment[3]; payments[0].timeToPayment = 30; payments[0].paymentAmount = 1.0; payments[1].timeToPayment = 60; payments[1].paymentAmount = 2.0; payments[2].timeToPayment = 90; payments[2].paymentAmount = 3.0; double pv = CLIBRARY.presentValue(payments); // also acceptable: double pv = CLIBRARY.presentValue(payments, payments.length); </code></pre> <p>How can this be accomplished?</p> <p>EDIT: to provide additional information, SWIG is a requirement because it is already used to incorporate simpler interfaces into the same Java codebase and supporting more than one approach for solving this problem will not be acceptable. Also, the signature of the C functions can't be changed and this example has been extremely simplified; the actual problem involves multiple C functions with signatures requiring arrays of structs (multiple arrays of different structs in some cases) and some of the structs are quite large, so I would really, really prefer not to have to break them up into equivalent arrays of primitive types.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload