COSC 1320 - C++ Programming
Bob Comer, Professor, CIT/CSC
Pointers and One-Dimensional Arrays
Read about pointer arithmetic before reading these notes.
If a pointer variable contains the address of an array element (array component), adding one to the pointer will give you the address of the following array element. For example, given:
int list[6] = { 10, 20, 30, 40, 50, 60 };
int *ptr;
ptr = &list[4];
Then ptr + 1 is the address of list[5].
Of course, if a pointer variable contains the address of an element in an array, dereferencing the pointer will give you the array element. For example, the following code will print the first element of the array, change it's value to 5, then print it again:
int list[6] = { 10, 20, 30, 40, 50, 60 };
int *ptr;
ptr = &list[0];
cout << *ptr << endl; // prints 10
*ptr = 5; // stores 5 in list[0]
cout << list[0] << endl; // prints 5
The name of an array used without a subscript is the address of the first element of the array. So:
&list[0]
is equivalent to listAlso,
&list[1]
is equivalent to list + 1Dereferencing both sides of these equivalencies, we get:
*&list[0]
is equivalent to *listBut the dereferencing operator (*) and the address of operator (&) are inverse operators. If you take the address of a variable and then dereference it, you get the variable back. In other words,
*&list[0]
is the same as list[0]So back to the equivalencies, they become:
list[0]
is equivalent to *listand in general:
list[n]
is equivalent to *(list+n)In fact, when you compile a C++ program, the C++ preprocessor translates the array notation into pointer notation. For this reason, arrays can always be processed using either array notation or pointer notation.
Now we are ready to give some examples of processing an array. The following examples all print the contents of the array.
// Example 1 - using array notation
int list[6] = { 10, 20, 30, 40, 50, 60 };
int index;
for ( index = 0; index < 6; index++ )
cout << list[index] << endl;
// Example 2 - straight-forward translation into pointer notation
int list[6] = { 10, 20, 30, 40, 50, 60 };
int index;
for ( index = 0; index < 6; index++ )
cout << *(list+index) << endl;
// Example 3 - using a pointer variable
int list[6] = { 10, 20, 30, 40, 50, 60 };
int index;
int *ptr = &list[0];
for ( index = 0; index < 6; index++ )
cout << *(ptr+index) << endl;
// Example 4 - another example using a pointer variable
int list[6] = { 10, 20, 30, 40, 50, 60 };
int *ptr;
for ( ptr = list; ptr < list + 6; ptr++ )
cout << *ptr << endl;
// Example 5 - using array notation with a pointer variable
int list[6] = { 10, 20, 30, 40, 50, 60 };
int index;
int *ptr = list;
for ( index = 0; index < 6; index++ )
cout << ptr[index] << endl;
Now I will re-write two of these examples as functions. Here is Example 1 as a function:
// Example 6 - a function using array notation
int list[6] = { 10, 20, 30, 40, 50, 60 };
void print( int list[] )
{
int index;
for ( index = 0; index < 6; index++ )
cout << list[index] << endl;
}
Here is example 4 re-written as a function. Note that when we pass an array to a function, we do not send a copy of the array values - we send the starting (or base) address of the array. This is the address of the first element of the array; that is, we pass a pointer to the first element in the array. So C++ allows us to declare any array parameter using pointer notation.
// Example 7 - another example using a pointer variable
int list[6] = { 10, 20, 30, 40, 50, 60 };
void print( int *list )
{
int *ptr;
for ( ptr = list; ptr < list + 6; ptr++ )
cout << *ptr << endl;
}
Executive Summary:
When you write code to process an array, you should normally use array notation. If you look at older C or C++ code, you may find code similar to Example 4.
Copyright: Ó 2002 by the Austin Community College.
Department of Computer Studies. All rights reserved.
Comments to: Bob Comer
Last updated: Dec. 6, 2002