• Post Reply Bookmark Topic Watch Topic
  • New Topic

passing back array from c to java

 
cindrella cradford
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi frnds i want to return an integer array fron c prog to java how do i
proceed with it .....i tried with some code but is is not
working......please tell me wat to do and where i m wrong
my code is
class IntArray {
private native int[] sumArray(int[] arr);
public static void main(String[] args) {
IntArray p = new IntArray();
int arr[] = new int[10];
int newarr[]=new int[10];
System.out.println("previous values of newarr");
for(int j=0;j<10;j++)
{arr[j]=j;
System.out.println(arr[j]);
}
newarr=p.sumArray(arr);

//for (int i = 0; i < 10; i++)
// {
//arr[i] = i;
//}
// int sum = p.sumArray(arr,newarr);
System.out.println("new values of newarr");
try{for(int j=0;j<10;j++)
System.out.println(newarr[j]);
}
catch(NullPointerException np){}
System.out.println("value of sum");
// System.out.println("sum = " + sum);
}
static {
System.loadLibrary("IntArray");
}



}


#include<stdio.h>
#include<jni.h>
#include"IntArray.h"
JNIEXPORT jintArray JNICALL Java_IntArray_sumArray
(JNIEnv *env, jobject obj, jintArray arr)
{
jint buf[10];
jint i, sum = 0;
(*env)->GetIntArrayRegion(env, arr, 0, 10, buf);
for (i = 0; i < 10; i++) {
buf[i]= buf[i]+2;
printf("%d",buf[i]);
}
return buf;
}

it is printing the values in c program but it is not retuning the newarr
 
Guy Allard
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try looking at

this example

Hope that helps.

Guy
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!