• Loading
    • How To Use Java Arrays In C?

      Previous Article:- How To Use Java Primitive Datatypes In C?

      The JNI equivalent of an array is jarray. To access an array from a Java program in a C program, you must copy the array into a buffer defined in the C program. The JNI provides two methods that enable you to access arrays from a Java program in C:

      Get<type>ArrayRegion: Copies the contents of an array into a buffer.

      Set<type>ArrayRegion: Copies the contents of a buffer into an array.

      For example, to copy the contents of an integer array, n, with 10 elements into a buffer, buf, you need to include the following in the C code:

      Code:
      (*exeenv)->GetIntArrayRegion(exeenv, n, 0, 10, buf)
      Accessing An Integer Array In C

      Code:
      class ArrayDemo 
      {
      	private native void disArray(int[] n);
      	public static void main(String[] args) 
      	{
      		ArrayDemo dem = new ArrayDemo();
      		//Initializing the array
      		int n[] = {45,78,35,34,65,32,98,11,3,67};
      		//Invoking the native method
      		dem.disArray(n);
      	}
      	static
      	{
      		//Loading the library
      		System.loadLibrary("ArrayDemo_lib");
      	}
      }
      This code shows how an array is initialized. The array is then passed as a parameter to a native method. The implementation of the native method is provided by the ArrayDemo_lib library.

      Following code shows how the header file that corresponds to the ArrayDemo class file is generated.

      Code:
      /* DO NOT EDIT THIS FILE - it is machine generated */
      #include <jni.h>
      /* Header for class ArrayDemo */
      #ifndef _Included_ArrayDemo
      #define _Included_ArrayDemo
      #ifdef __cplusplus
      extern "C" 
      {
      	#endif
      	/*
      	 * Class:     ArrayDemo
      	 * Method:    disArray
      	 * Signature: ([I)V
      	 */
      	JNIEXPORT void JNICALL Java_ArrayDemo_disArray (JNIEnv *, jobject, jintArray);
      	#ifdef __cplusplus
      }
      #endif
      #endif
      This code shows how to generate the header file for the ArrayDemo class file.

      Implementation of the disArray Method
      Code:
      #include <stdio.h>
      #include <jni.h>
      #include "ArrayDemo.h"
      JNIEXPORT void JNICALL 
      Java_ArrayDemo_disArray(JNIEnv *exeenv, jobject javaobj, jintArray n)
      {
           jint *buf;
           jint i;
      	 printf("Printing the values of the array in C\n");
      	 //Storing the elements of the array in a buffer
           buf = (*exeenv)->GetIntArrayElements(exeenv, n, NULL);
           if (buf == NULL) 
      	 {
               return; /* exception occurred */
           	 }
           for (i=0; i<10; i++) 
      	 {
               //Printing the array elements
               printf("%d\n",buf[i]);
           	 }
           (*exeenv)->ReleaseIntArrayElements(exeenv, n, buf, 0);
           
       }
      The above code shows how an array of integers from Java code are accepted and stored in a buffer defined in C. The array is then depicted in the C program.

      Next Article:- How To Use Java Strings In C?
    • Currently Active UsersCurrently Active Users

      There are currently 57 users online. 4 members and 53 guests

      Most users ever online was 323, 11-23-2011 at 08:47 AM.

      1. atri2k5,
      2. pppp11,
      3. reason,
      4. richa bhuria