Converting Pointers that Operate on Arrays in C++ to Java
By: Charles in C++ Tutorials on 2007-09-15
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.
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Calculating total based on the given quantity and price in C++
Sorting an array of Strings in C++
Matrix using nested for loops in C++
Compute the square root of the sum of the squares of an array in C++
Calculate average using Two-Dimensional Array in C++
Two-Dimensional Array Manipulation in C++
Compiling and Linking Multiple Source Files in C++
Escape Sequences for Nonprintable Characters in C++
Using the Built-in Arithmetic Types in C++
Comments