cjava-native-interfaceuint16

Convert uint16 to jint and back JNI


everyone. I am writing under Android using JNI. I need to pass an "interval" into my C foo() as uint16, but after that i use the same "interval" in return to Java.

C:

 static jint func (JNIEnv* env, jobject object,jstring first)
        {
        /*...action...*/
        uint16  interval;
        jint result = foo (ifirst, &interval);
        return result < 0 ? result : interval;
        }

I have error below

error: operands to ?: have different types 'jint {aka int}' and 'uint16* {aka short unsigned int*}'

how can i use 'interval' argument to avoid an error and continue correct work of program?


Solution

  • Based on the code, you need to add a cast to ensure that both conditions of the ?: operator are the same type.

     static jint func (JNIEnv* env, jobject object,jstring first)
     {
        /*...action...*/
        uint16  interval;
        jint result = foo (ifirst, &interval);
        return result < 0 ? result : (jint)interval;
     }