noMAD
noMAD

Reputation: 7844

Convert code with pointers in C to Java code

I am having some difficulty in understanding how to write the below piece of code using String or char[] in Java.

void xyz(char *a, int startIndex, int endIndex)
{
   int j;
   for (j = startIndex; j <= endIndex; j++)
   {
       doThis((a+startIndex), (a+j));
       xyz(a, startIndex+1, endIndex);
   }
}

Here char *a points to the starting location of the char name[]

The above are just some random functions, but I just want the logic of how to use char* and character index char[] in Java

Upvotes: 1

Views: 1880

Answers (4)

josefx
josefx

Reputation: 15656

If your method modifies the values you cant use String as that is immutable, you can use StringBuilder instead.

If your methods already rely on char arrays and you need the offsets you can use a CharBuffer to wrap an array. It does not support String operations but supports views for sub ranges, which seems to be what you use in the doThis() method.

Upvotes: 0

Louis Wasserman
Louis Wasserman

Reputation: 198033

Based on the rephrased question from the comment thread:

You cannot change the characters of a Java String. If you need to modify a sequence of characters, use StringBuilder, which supports setCharAt(int, char), insert(int, char), and append(char). You can use new StringBuilder(myString) to convert a String to a StringBuilder, and stringBuilder.toString() to convert back.

This is perfectly legit Java code -- it's not code smelly, it's just the way you work with mutable character sequences.

Upvotes: 3

Gus
Gus

Reputation: 6871

Simple Answer:

You can't do exactly that. Java is pass by reference only. You don't have access to memory location information, so you can't do arithmetic with it.

Longer Answer:

It looks like you are passing in a string for manipulation. You have several options to simulate that.

  1. You can convert the string to an array of characters and then pass in a char[]. If your manipulations are not any sort of standard string operation and completely custom this is probably what you need to do. Keep in mind that you can't change the size of the array passed in, nor can you have a point at a new array after the function completes. (again, only pass by value). Only the values of the existing elements of the array can be modified.
  2. You can pass in the String and use the String methods, such as subString() (which your begin and end indexes seem to suggest, but this may not meet your needs. Note that strings are immutable however, and you can only get a result out via the return statement.
  3. If you really need to modify the contents of the object passed in you can pass a StringBuilder, StringBuffer or CharBuffer object and modify away.

There's a hack that can also be used to circumvent pass by reference, but it's poor style except in special situations. Pass in an array of whatever you need to modify, so in this case an array of array of characters would allow you to set a new sub-array, and effectively acheive pass by reference, but try not to do this :)

Upvotes: 0

Erica
Erica

Reputation: 2251

A char* in C is, as you noted, pointing to the start of your character array (which is how C manages Strings).

In C the size of a char is one byte, and pointers always point to the start of a byte. Your C String is an array of characters, so adding 1 to a pointer moves the start of your string right by one character.

That means that the C code:

char *a;
// Set the String here
a = a + 1;

translates in Java to something like:

String a;
// Set the String here
a = a.substring(1);

or if you are using a char array:

char[] a;
// Set the array contents here
char[] copyTo = new char[a.length];
System.arraycopy(a, 1, copyTo, 0, a.length);
a = copyTo;

Java will be a bit more careful of protecting you that C will be though. For instance, if you have a zero length string, the C code has the potential to either segfault (crashing the application) or give you a gibberish string full of memory junk (then, eventually, crash the application), whereas the Java code will throw an exception (normally an IndexOutOfBoundsException) which you can, hopefully, handle cleanly.

Remember though, that String in Java are immutable. You cannot change them, you can only create new Strings. Fortunately, String has several built in functions which allow you to do a lot of the standard actions, like replace part of the String with another and return the result. A character array is mutable, and you can change the characters within them, but you will lose a lot of the nice benefits you get from using the proper String class.

Upvotes: 3

Related Questions