What`s the Address? Pointers - Passing an Array Using Pointer Notation
(Page 5 of 9 )
In Chapter 10, we employed the following program that used one function to assign values to the array and another function to display values from the array, rather than doing all that work in the main function.
#include <iostream>
using namespace std;
void assignValues(int[], int);
void displayValues(int[], int);
const int MAX = 3;
int main ()
{
int testScore[MAX];
assignValues(testScore, MAX);
displayValues(testScore, MAX);
return 0;
}
void assignValues(int tests[], int num)
{
for (int i = 0; i < num; i++)
{
cout << "Enter test score #" << i + 1 << ": ";
cin >> tests[i];
}
}
void displayValues(int scores[], int elems)
{
for (int i = 0; i < elems; i++)
{
cout << "Test score #" << i + 1 << ": "
<< scores[i] << endl;
}
}
As discussed in Chapter 10, the two functions, assignValues and displayValues, passed their first argument, the array, by address. An argument passed by address can be changed in the calling function (here main) by the called function (here assignValues) just as if the argument had been passed by reference. Thus, the assignValues function changed the value of the testScore array in main by assigning values to the elements of that array.
The function prototypes and headers of the assignValues and displayValues functions used a subscript [] to indicate that an array is being passed. However, you can also use pointer notation—for instance, asterisk * instead of a subscript []—as the following example demonstrates:
#include <iostream>
using namespace std;
void assignValues(int*, int);
void displayValues(int*, int);
const int MAX = 3;
int main ()
{
int testScore[MAX];
assignValues(testScore, MAX);
displayValues(testScore, MAX);
return 0;
}
void assignValues(int* tests, int num)
{
for (int i = 0; i < num; i++)
{
cout << "Enter test score #" << i + 1 << ": ";
cin >> tests[i];
}
}
void displayValues(int* scores, int elems)
{
for (int i = 0; i < elems; i++)
{
cout << "Test score #" << i + 1 << ": "
<< scores[i] << endl;
}
}
The following comparison of the prototypes of the assignValues function using subscript and pointer notation, respectively, shows that the only difference is whether a subscript [] or an asterisk * is used to denote that the argument is an array:
void assignValues(int[], int);
void assignValues(int*, int);
Similarly, the following comparison of the function headers of the assignValues function using subscript and pointer notation, respectively, shows that the only difference is whether a subscript [] or an asterisk * is used to denote that the argument is an array. This time, however, the asterisk precedes the variable name, whereas the subscript follows the variable name.
void assignValues(int tests[], int num)
void assignValues(int* tests, int num)
Whether you use subscript or pointer notation to pass an array really is a matter of preference. There is no programming advantage one way or the other. However, the next section discusses a situation in which subscript notation is not an option, so pointer notation is the only choice.
Next: Passing a Single Variable Using Pointer Notation >>
More C++ Articles
More By McGraw-Hill/Osborne
|
This article is excerpted from chapter 11 of the book C++ DeMYSTifieD, written by Jeff Kent (McGraw-Hill, 2004; ISBN: 0072253703). Check it out at your favorite bookstore. Buy this book now.
|
|