Section 5.2, 5.3, & 10.2 of Absolute C++.
There are many situations in which it's natural to ask for functions that have arrays as arguments or that return arrays ... or both! This class is just going to look at functions that operate on arrays, and the few new concepts that arise with them. In reality, there's nothing new at all ... it's just that the consequences of the fact that it is the pointers to the arrays that get manipulated in our programs, not the arrays themselves may not be so obvious.
We'll look at this stuff first from the perspective of our simple program from last class for computing averages. Here's that program. We're going to try and break it up into separate components using functions. Essentially we'd like the bulk of the program to consist of these two function calls:
// Create array A and read N int's from input
int *A = readints(N,cin);
// Compute average
double average = sum(A,N) / double(N);
In defining these two functions, we'll see how arrays get passed in and out of functions.
The first of our two functions is
int* readints(int N, istream &IN);.
This function should return an array of
ints, populated with values
read in from
basics of the function should be fairly straightforward. The important thing is
to understand what happens! So, here's the function definition:
int* readints(int N, istream &IN)
// Create the array
int* B = new int[N];
// Read values into the array
for(int i = 0; i < N; i++)
IN >> B[i];
// Return pointer to array
And here's a pictorial depiction of what goes on.
Notice that what gets returned from the function is a pointer, not an
actual array. The array of
does not disappear when the function call returns, because memory allocated
new has no scope or lifetime
... it just sticks around until the end of the program, or until it's
explicitly destroyed with a call to
which we'll discuss later.
So, to sum up, functions can create arrays with
new and return them by returning a
pointer to the block of memory allocated by the call to
Arrays can be passed into functions by passing a pointer to the block of memory that comprises the array. Notice what this means as far as pass-by-value is concerned: the pointer is what gets passed, so your function gets a copy of the pointer, but the copy points to the same array in memory, so when you modify the array in the function you modify the same array you have back in the calling function. Thus, some people will say that arrays are always passed by reference in C++. Well ... really what happens is that arrays themselves are not passed, rather pointers to the arrays are what we pass, and receiving a copy of the pointer still allows us to modify the actual array.
So, getting back to our example, we need to
implement the function
int sum(int* A, int
N);. Notice that when we pass an array to a function we always
have to pass the length of the array along with it, otherwise we have no idea
how many elements are in the array pointed to by
A. Hopefully the definition of this function looks
int sum(int* A, int N)
int total = 0;
for(int i = 0; i < N; i++)
total = total + A[i];
The question is this: What really goes on when we run it? Hopefully the pictures below, which illustrate what goes on in a sample run of the above function, will make clear to you what really goes on when arrays are passed to functions.
Hopefully the above functions make it pretty clear that if you modify any element within an array passed to a function, you're actually modifying the same array that the caller of the function is looking at.
contains predicate. Write function
bool contains(string *A, int N, string s);
that tells you whether or not the string
is contained in the array
getarray function. Write a function
int* getarray(int N, int x); that
allocates and returns a pointer to an array of
each initialized to the value
shiftleft function. Write a function
double shiftleft(double *A, int N, double
x); that shifts all the elements in array
A to the left by 1 position, puts the
x in the rightmost
position in the array, and returns the value that had previously been in the
Assoc Prof Christopher Brown
Last modified by LT M. Johnson 10/12/2007 03:41:37 PM