我有以下c代码:
#include <stdio.h>
struct Second {
int a_number;
};
struct Top {
struct Second second;
};
void lets_go(struct Top *top) {
printf("The number is %d\n", top->second.a_number);
}
我想从Java做到这一点:
int main(void) {
struct Top top = {{8}};
lets_go(&top);
}
我也想使用jnr-ffi,所以我查看了测试并最终得到了这个:
package structs.playing;
import structs.playing.Program.Test.Top;
import structs.playing.Program.Test.Second;
import jnr.ffi.LibraryLoader;
import jnr.ffi.Runtime;
import jnr.ffi.Struct;
public final class Program {
public static interface Test {
void lets_go(Top top);
public static final class Second extends Struct {
public final Signed32 a_number = new Signed32();
public Second(final Runtime runtime) {
super(runtime);
}
}
public static final class Top extends Struct {
public Second second;
public Top(final Runtime runtime) {
super(runtime);
}
}
}
public static void main(final String[] args) {
Test test = LibraryLoader.create(Test.class).load("test");
Runtime runtime = Runtime.getRuntime(test);
Top top = new Top(runtime);
Second second = new Second(runtime);
top.second = second;
second.a_number.set(7);
test.lets_go(top);
}
}
问题是a_number
根本没有设置的值,所以我在输出中得到一个垃圾值,例如:
The number is 46645760
那么我如何获得与我的 C 代码相同的内容呢?