[英]Passing pointer to pointer to float from Java through JNA to a C dynamic library
[英]JNA passing the equivalent of swift pointer from Java Android to C
我在嘗試將 Java 中的 Swift 代碼的實現傳遞給 C 時有點卡住了。 如果我使用 JNA 在 Java 和 Matlab 生成的 C 代碼之間集成,我將不勝感激。
這是 Swift 調用:
GetResult(uptime, &result_out)
它映射到 C 代碼:
void GetResult(double time, Result_t *Result)
C 結構 Result_t 在這里太大了,但它是 double 和 int 的組合。
我了解 &result_out 是 swift 指針? How is it best to go about implementing the equivalent of the Swift pointer in Java and then be able to pass it to C that is expecting a pointer to the Result_t which as i mentioned is a structure made up of doubles and ints.
&
表示變量的 memory 地址。 在這種情況下,它表示Result
結構的 memory 地址。
在 C 中,您可以看到用於表示同一事物的*
符號:作為第二個參數傳遞的變量是基礎數據的引用/內存地址。
這些通常成對出現:如果您定義了一個變量foo
,您可以輕松地將&foo
傳遞給任何期望指向foo
的指針的方法,而該方法本身使用*
表示法讓您知道它需要該指針。
對於許多 JNA 結構,您需要密切注意這些符號。 例如,知道是傳遞一個int
還是IntByReference
是關鍵。 但幸運的是(為了便於編寫代碼,或者不幸的是為了一致性)JNA Structure
class 的行為是不同的。 底線是,默認情況下,當Structure
作為方法/函數(最常見的應用程序)的參數包含時,將使用指針或結構的ByReference
版本。 因此,在這種特殊情況下,假設您已經映射了一個class Result extends Structure
以及所有信息,您可以簡單地傳遞result
where result = new Result();
.
有多種方法可以使用ByReference
和ByValue
來覆蓋此默認行為,但這超出了您問題的 scope 范圍。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.