Converting Pointers that Operate’ on Arrays
Conceptually, converting C++-style pointer-based array accessing into the equivalent java-compatible array indexing is straightforward-simply substitute the appropriate array-indexing statements. However, in practice this may require some thought. ” Pointer-based array accessing can be a bit difficult to follow, because the normal C++ coding style encourages rather dense, complex expressions. For example, this short C..+ program copies the contents of one army to another. It uses 0 to mark the end of the arrays. Pay special attention to the pointer expressions. Even in this simple example, if you did not know that this program copied the contents of bums to copy (and later displayed the arrays); it would require some careful thought before you were completely sure that you knew what the code was doing. still requires a moment of thought to decipher its exact operation. One of the advantages of Java is that it does not encourage the creation of such expressions in the first place. Here is the Java version bf the program. As you can see, its purpose and effects are transparent. Much ~++ code is sprinkled with obscure, difficult to understand pointer expressions. While these expressions do tend to’increase speed of execution, they are one of the most troubling issues confronting the maintenance of C++ programs. They will also present difficulty when you convert the code to Java. When you are confronted with a complex pointer expression, it is sometimes useful to begin by breaking it into its sub expressions so that its exact operation becomes clear.