COSC 1337 – Programming Fundamentals II
One-Dimensional Arrays and Pointers
This document reviews and in some cases expands on the material on pointers from sections 10.1 through 10.6 of the Gaddis textbook. Please read these sections of the textbook before reading this document.
Concept: The name of a one-dimensional array is the address of the first element of the array. The address of the first element of an array is called the base address of the array.
Given the array:
int list[6] = { 10, 20, 30, 40, 50, 60 };
Perhaps the best way to describe the data type for list is to say that list is an array of int (integers). However, in C++ there is a close relationship between arrays and pointers. The array name is actualy a pointer. It is a pointer to the first element in the array.
We can use the subscript operator to access the first element of the array:
cout << list[0]; // prints 10 list[0] = 15; cout << list[0]; // prints 15
Alternatively, we can use the array name and the dereference operator to access the first element:
cout << *list; // prints 10 *list = 15; cout << *list; // prints 15
Concept: Pointer arithmetic gives us a way to calculate the address of any element in an array. If a pointer points to an integer, adding one to the pointer gives us the address of the next integer.
Given the declaration for array list above, we know that list is the address of the first element in the array and list+1 is the address of the second element in the array.
We can use the subscript operator to access the second element of the array:
cout << list[1]; // prints 20 list[1] = 25; cout << list[1]; // prints 25
Alternatively, we can use the array name and the dereference operator to access the second element:
cout << *(list+1); // prints 20 *(list+1) = 25; cout << *(list+1); // prints 25
Concept: The subscript operator in C++ actually preforms two operations. It adds the index to the base address of the array, then it dereferences the result.
This means that:
list[n] is defined as *(list+n)
In fact, the C++ preprocessor actually replaces all subscript operations with pointer operations before the code is compiled.
Concept: When you pass an array to a function, a copy of the array is not created. The array parameter receives a copy of the base address of the array (the address of the first element of the array).
Say we have a function to print the elements of an array (one per line):
void print( int list[], int size ) { for ( int index = 0; index < size; index++ ) cout << list[index] << endl; }
When function print is called, parameter list receives a copy of the base address of the array specified in the call. This base address and parameter size give the function the information it needs to locate all elements of the array.
In this function, parameter list receives the address of the first element of the array that is passed into the function. This means that list is a pointer to an integer. The array parameter can be re-written in pointer notation:
void print( int* list, int size ) { for ( int index = 0; index < size; index++ ) cout << list[index] << endl; }
Concept: When you write a function to process an array, normally you should use array notation because it is realtively simple and easy to understand. However, many utility functions included in C++ require that you understand how pointers can be used to access arrays and blocks of memory. So it is important that you understand how pointers can be used with arrays.
Following are three versions of the print function that use pointer notation instead of array notation.
Example 1 - uses a straight-forward translation of array notation into pointer notation
void print( int* list, int size ) { for ( int index = 0; index < size; index++ ) cout << *(list+index) << endl; }
Example 2 - uses an external pointer variable
void print( int* list, int size ) { int *ptr = list; for ( int index = 0; index < size; index++ ) { cout << *ptr << endl; ptr++; } }
Example 3 - another way to use an external pointer variable
void print( int* list, int size ) { for ( int *ptr = list; ptr < list+size; ptr++ ) cout << *ptr << endl; }
© Austin Community College 2016. The content on this page is licensed under a
Creative Commons Attribution 4.0 International License.
Last updated: July 7, 2015