Code in C:
typedef struct{
int a;
int b;
}NODE;
NODE *node;
NODE* add(int c){
node->a=node->a+10;
c=node->a+node->b;
node->a=c;
return node;
}
How do I declare my native function “add” with structure pointer as return type in Java using JNI?
It’s not possible for your Java application to meaningfully process structures, pointers or even pointers to structures returned from your native code. Therefore, JNI does not permit this.
However, if you want to treat your pointer to structure as an opaque handle that is simply passed around in Java but only ever processed in native code then you can define the native method like:
Or
Which one you should use will depend on if your native code uses 32bit or 64bit pointers. Your native code will then have to correctly cast from your pointer to a jint or jlong as appropriate before returning it.