Converting Pointers that Operate on Arrays in C++ to Java

By: Charles Emailed: 1674 times Printed: 2159 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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 array 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 nums 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.

// Copy an array in C++ using pointers.
#include <iostream>
using namespace std;
int main()
{
int nums[] = {10, 12, 24, 45, 23, 19, 44,
88, 99, 65, 76, 12, 89, 0};
int copy[20];
int *p1, *p2; // integer pointers
// copy array
p1 = nums; // p1 points to start of nums array
p2 = copy;
 


while(*p1) *p2++ = *p1++;
*p2 = 0; // terminate copy with zero
// Display contents of each array.
cout << "Here is the original array:\\n";
p1 = nums;
while(*p1) cout << *p1++ << " ";
cout << endl;
cout << "Here is the copy:\\n";
p1 = copy;
while(*p1) cout << *p1++ << " ";
cout << endl;
return 0;
}

Even though it is quite simple for C++ code, at first glance the line

while(*p1) *p2++ = *p1++;

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 of the program. As you can see, its purpose and effects are transparent.

// Array copy without pointers using Java.
class CopyArray {
public static void main(String args[]) {
int nums[] = {10, 12, 24, 45, 23, 19, 44,
88, 99, 65, 76, 12, 89, 0};
int copy[] = new int[14];
int i;

// copy array
for(i=0; nums[i]!=0; i++)
copy[i] = nums[i];
nums[i] = 0; // terminate copy with zero

// Display contents of each array.
System.out.println("Here is the original array:");
for(i=0; nums[i]!=0; i++)
System.out.print(nums[i] + " ");
System.out.println();
System.out.println("Here is the copy:");
for(i=0; nums[i]!=0; i++)
System.out.print(copy[i] + " ");
System.out.println();
}
}

Both versions of the program produce the following results:

Here is the original array:
10 12 24 45 23 19 44 88 99 65 76 12 89
Here is the copy:
10 12 24 45 23 19 44 88 99 65 76 12 89

Much C++ 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 subexpressions so that its exact operation becomes clear.


C++ Home | All C++ Tutorials | Latest C++ Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Charles
arraycopy example in Java
Action listeners in JSF
Automatically Including Preludes and Codas in JSP
Using realloc() Function in C
Handling multiple buttons in HTML Form in Struts
Fopen and Getc implementation program in C
Pointers to Structures example program in C
Pointers vs. Multi-dimensional Arrays in C
getch and ungetch in C
Bitwise Operators in C
Variables and Arithmetic Expressions in C
Differences between JavaServer Faces technology and Struts
assert() example program in C++
'double buffering' Sample program in J2ME
Converting Default Function Arguments in C++ to Java

More Tutorials in C++
Two-Dimensional Array Manipulation in C++
Calculate average using Two-Dimensional Array in C++
Compute the square root of the sum of the squares of an array in C++
Matrix using nested for loops in C++
Sorting an array of Strings in C++
Calculating total based on the given quantity and price in C++
Compiling and Linking Multiple Source Files in C++
Enumerations in C++
Program to add two numbers in C++
Comments in C++
while loop in C++
for loop in C++
Programming errors a compiler will detect in C++
if in C++
Using the Built-in Arithmetic Types in C++

More Latest News
Most Viewed Articles (in C++ )
Public versus Private members in C++
Dot (.) vs Arrow (->) to access data members in C++
strcat() and strncat() sample program in C++
while loop in C++
Two-Dimensional Array Manipulation in C++
const Member Functions in C++
while (1) Loops in C++
Compiling and Linking Multiple Source Files in C++
Calculate average using Two-Dimensional Array in C++
Sorting an array of Strings in C++
Calculating total based on the given quantity and price in C++
Using Comments in a C++ Program
The indirection operator (*) - dereference operator.
Stray or Dangling Pointers in C++
Converting Default Function Arguments in C++ to Java
Most Emailed Articles (in C++)
Looping with the keyword goto in C++
Difference between Procedural, Structured, and Object-Oriented Programming
Operator Precedence in C++
Using peek() and putback() with cin in C++
Function overloading in C++
Default arguments in C++
Lvalues and Rvalues in C++
Compiling and Linking Multiple Source Files in C++
C++ and Object-Oriented Programming
Getting Started with C++
Tutorial on Classes and Members in C++
Public versus Private members in C++
for Loops in C++
Using switch Statements in C++
How to handle Exceptions in C++