GeneralError
GeneralError

Reputation: 27

Android - NDK - C++ - cannot initialize a variable of type 'int *' with an rvalue of type 'jlong *' (aka 'long long *')

I asked a similar question about char to jsstring ... but now I have a problem with an int to jlongArray, which I just can't figure out :/

I get the following error:

Error:(290, 10) error: cannot initialize a variable of type 'int *' with an rvalue of type 'jlong *' (aka 'long long *')

on this line:

JNIEXPORT void Java_de_meetspot_ndktest_MainActivity_LoadPlayerA(JNIEnv *javaEnvironment, jobject self, jstring audioPath, jlongArray offsetAndLength) {
   const char* audio = javaEnvironment->GetStringUTFChars(audioPath, JNI_FALSE);
   int* params = javaEnvironment->GetLongArrayElements(offsetAndLength, JNI_FALSE);
   example->LoadPlayerA(audio, params);
}

this is the declaration:

void LoadPlayerA(const char *audioPath, int *params);

can someone help me out?

Upvotes: 0

Views: 2801

Answers (2)

GeneralError
GeneralError

Reputation: 27

Thanks to Michael :) ..

here is how i did it:

 jlong *longParams = javaEnvironment->GetLongArrayElements(params, JNI_FALSE);
   int arr[6];
   for (int n = 0; n < 6; n++) arr[n] = longParams[n];
   javaEnvironment->ReleaseLongArrayElements(params, longParams, JNI_ABORT);

Upvotes: 0

Michael
Michael

Reputation: 58467

long in Java is a signed 64-bit integer type. A C++ int on the other hand is only defined as being "guaranteed to have a width of at least 16 bits".

Lets's assume that your ints are 32-bit (that's a very common scenario): so now you've got a pointer to a bunch of data where each element is 64 bits, and you're trying to pass that to a function that expects a pointer to 32-bit data. That's obviously not going to work, even if the compiler had allowed it.

Some options for you:

  • Change LoadPlayerA to take a jlong* or int64_t* (and change any code in LoadPlayerA that relies on the incoming data being of type int).
  • Change the Java code to pass the data as an int[] instead of a long[], and change LoadPlayerA to take a jint* or int32_t*.
  • Allocate an array/vector of type int in your JNI function and convert the data from jlong to int before passing it to LoadPlayerA.

Upvotes: 2

Related Questions