translation from c to java. array trouble

G

Gyro

Hi all!

I fall into trouble. Need to port this c code:

call_some_c_function(arr + 2, b, c); // arr is array

Call as above legal in c-language, but illegal in Java. C programmer
just passed array part into function. How i do it in Java?
I don`t want to rewrite all functions which deals with arrays
implementing indexing with base, there thousands array references...
 
H

Harish

pointer arithmetic is not allowed in java.
you'll have to change the code arr + 2 to arr
I don`t want to rewrite all functions which deals with arrays
implementing indexing with base, there thousands array references...

you dont have any other option but to rewrite.
you'll have more issues...don't expect a C snippet to work in java without
any changes....they are not the same..

instead of porting, may be you can use JNI..which is a mechanism to call
functions in a .dll/.so
 
X

xarax

Harish said:
pointer arithmetic is not allowed in java.
you'll have to change the code arr + 2 to arr
I don`t want to rewrite all functions which deals with arrays
implementing indexing with base, there thousands array references...

you dont have any other option but to rewrite.
you'll have more issues...don't expect a C snippet to work in java without
any changes....they are not the same..

instead of porting, may be you can use JNI..which is a mechanism to call
functions in a .dll/.so


Gyro said:
Hi all!

I fall into trouble. Need to port this c code:

call_some_c_function(arr + 2, b, c); // arr is array

Call as above legal in c-language, but illegal in Java. C programmer
just passed array part into function. How i do it in Java?
I don`t want to rewrite all functions which deals with arrays
implementing indexing with base, there thousands array references...


The typical way of slicing an array in Java is
to specify additional parameters for the starting
index (inclusive) and the ending index (exclusive).

public void callSomeJavaFunction(int[] arr, int start, int end, int b, int c)
{
/* use arr[start] as the first element. */
/* use arr[end-1] as the last element. */
/* the number of elements is (end-start).*/
}
 
C

Chris Smith

xarax said:
The typical way of slicing an array in Java is
to specify additional parameters for the starting
index (inclusive) and the ending index (exclusive).

public void callSomeJavaFunction(int[] arr, int start, int end, int b, int c)

No argument with the general concept. However, the almost universal
convention within the standard API and almost any other code I've used
is to specify the starting index and length. If you use starting and
ending indexes, be sure to document this plainly and obviously in some
location that will be seen by anyone using your code.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
J

Jon Caldwell

Harish said:
pointer arithmetic is not allowed in java.
you'll have to change the code arr + 2 to arr

I don`t want to rewrite all functions which deals with arrays
implementing indexing with base, there thousands array references...


you dont have any other option but to rewrite.
you'll have more issues...don't expect a C snippet to work in java without
any changes....they are not the same..

instead of porting, may be you can use JNI..which is a mechanism to call
functions in a .dll/.so


Hi all!

I fall into trouble. Need to port this c code:

call_some_c_function(arr + 2, b, c); // arr is array

Call as above legal in c-language, but illegal in Java. C programmer
just passed array part into function. How i do it in Java?
I don`t want to rewrite all functions which deals with arrays
implementing indexing with base, there thousands array references...


The common convention is Java is to pass offset and length along with
the array.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top