title: Using JNI to pass a C pointer to a double array to Java
body: <p>I am trying to use an external C library in a Java program via JNI. I need to use two functions, the first is a calculation in C and stores some results in a double array, the second function then returns the pointer to this double array. </p>
  
  <p>My questions is now how to access/use this double array in Java.</p>
  
  <p>JNI Code:</p>
  
  <pre><code>JNIEXPORT jdoubleArray JNICALL Java_rna_jni_ViennaRNAInterface_getBasePairingProbabilities(JNIEnv *env, jobject thisObj, jstring sequence){
  
      jdoubleArray dArray;
      const char *seq= (*env)-&gt;GetStringUTFChars(env,sequence,0);
  
      // Calculates a single value, stores important results in double array
      pf_fold_par(seq,NULL,NULL,1,0,0); 
  
      // export_bppm() returns pointer to double array
      double* pr = export_bppm();
  
      // Processing of double array
  
      return dArray;
  }
  </code></pre>
  
  <p>The Code in the C library looks essentially like this,
  <code>FLT_OR_DBL</code> is defined as either Float or Double:</p>
  
  <pre><code>PRIVATE FLT_OR_DBL  *probs=NULL;
  
  PRIVATE void get_arrays(unsigned int length){
      size  = sizeof(FLT_OR_DBL) * ((length+1)*(length+2)/2);
      probs = (FLT_OR_DBL *) space(size);
  }
  
  PUBLIC float pf_fold_par(const char *sequence,char *structure, pf_paramT *parameters,int calculate_bppm,int is_constrained, int is_circular){
      float result;
      int n = (int) strlen(sequence);
  
      get_arrays(n);
  
      /*
      * Calculating result
      */
  
      // Fill double array
      pf_create_bppm(sequence, structure); 
      pr = probs;
  
      return result;
  }
  </code></pre>
  
  <p>If necessary I can provide the exact implementation in the library as well.</p>
  
  <p>\edit: Exact Implementation at <a href="http://pastebin.com/ayGg2HBR" rel="nofollow">http://pastebin.com/ayGg2HBR</a></p>