• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

passing back array from c to java

 
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
 
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try looking at

this example

Hope that helps.

Guy
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic