以下java代码段调用c函数:
Java代码
String s[] = new String[10];
for(int i=0;i<10;i++) {
s[i] = "s";
}
o.printArrayLength(s); // prints the array length from c code
C代码:
void Java_Package_CallMethodOfSuperClass_printArrayLength
(JNIEnv *env, jobject obj, jobjectArray arr) {
jsize size = (*env)->GetArrayLength(env,arr);
printf("
");
printf("Size of array from C : ");
printf("%d",size);
jcharArray chrArr = (*env)->GetCharArrayElements(env,arr,NULL);
char Arr[11];
strcpy(Arr,chrArr);
int i = 0;
printf("Now printing the array declared in java from c :");
printf("SIZE SIZE SIZE SIZE : %d",size);
for(i=0;i<size;i++) {
printf("Inside for loop !");
printf("%s",Arr[i]);
}
//jobjectArray obArr = (*env)->NewObjectArray(env,size,(*env)->FindClass(env,"[L"),NULL);
//(*env)->SetObjectArrayElement(env,obArr,0,chrArr);
}
当发生上述情况时,我会看到以下输出:
Size of array from C : 10
Now printing the array declared in java from c :
SIZE SIZE SIZE SIZE : -549339085 // What the heck !
为什么我得到的大小值不同于10。值10在前一行打印。
接下来,如果我将for循环中的复选框保持为10,那么数组也不会打印出来。为什么会这样?相反,我得到了一个致命的错误。