C++ Primer For Java Programmers


Arrays And Pointers

<< Arrays | Table of Contents | Multi Dimensional Arrays >>
(:toc:) As indicated earlier, static arrays are referenced via a pointer variable. In C and C++, any pointer variable can be used as if it were an array because arrays and pointers are interchangeable. When the gradeList array in the previous section is created, it is given the type (int *) which of course is a pointer to an integer.

Pointer Assignment

Since the variable gradeList is actually a pointer to an int, we can assign that variable to another pointer variable which points to an int

(:source lang=cpp:)
int *myPtr = gradeList;

which results in the following

It does not matter if the pointer assigned to myPtr points to a single int or an array of ints. What matters is that the right hand side of the assignment must be a pointer to an int which gradeList of course is.

(:note info:) While you can assign a static array pointer to another pointer variable, the reverse is not legal since a static array pointer is marked as constant by the compiler. In fact, a static array pointer is a special case and there is no actual pointer variable created, but this is the easiest way to understand C/C++ arrays and pointers. (:noteend:)

To direct a pointer to a specific element of an array use the address-of operator

(:source lang=cpp:)
int *thirdElement = &gradeList[ 2 ];

Pointer Subscripts

We can now use the new pointer myPtr as if it were an array and thus access elements using the subscript notation

(:source lang=cpp:)
int sum = 0;
for( int i = 0; i < NUM_GRADES; i++ )
  sum += myPtr[ i ];

When a subscript is applied to a pointer variable, C/C++ simply computes the actual address of the element based on the address in the pointer variable (myPtr), the size of the element type (4) and the element index (i) using the following equation

addr = baseAddr + typeSize * index

Since no range checking is performed, it is up to you to make sure your subscripts do not result in references that are out of bounds.

(:note info:) This feature of C/C++ in which arrays are referenced by pointers corresponds to how arrays are referenced at the machine-language level. (:noteend:)

Arrays as Parameters

An alternate approach for specifying an array as an argument to function is to simply specify a pointer

(:source lang=cpp:)
float avgGrade( int *theGrades, int num ) 
{
  int sum = 0;
  for( int i = 0; i < num; i++ ) 
    sum += theGrades[ i ];

  return sum / float( num );
}

This works since an array is always referenced by a pointer and subscripts can be applied to any pointer variable. The C++ approach for specifying arrays as parameters is discussed in the [[Arrays] section.

Pointer Arithmetic

Another special feature of pointers and arrays in C/C++ is the ability to perform pointer arithmetic. Consider the following example which is a modified version of an earlier code segment

(:source lang=cpp:)
int *myPtr = gradeList;
for( int i = 0; i < NUM_GRADES; i++ ) {
  printf( "%d\n", *myPtr );
  myPtr += 1;
}

In this version, we no longer use a subscript to reference the array element. Instead, we

  • dereference myPtr directly within the printf() function,
  • increment the pointer myPtr by one each time through the loop.

When a pointer is incremented, the address stored in that variable is incremented by the data type size to which the pointer references. In this case, the address is incremented by 4 since our pointer is a pointer to an int. This results in moving the pointer down to the next element in the array. Thus, after the first iteration of the for loop, the result will be as shown below

Subtraction and multiplication can also be used with pointers to adjust the address stored in the variable.

Dynamic Arrays

Arrays in C++ can also be created at runtime just as they are in Java. There are two approachs to creating dynamic arrays: C++ specific dynamic allocation or the original C allocation approach.

To create a dynamic array using the C++ approach, the new operator is used as it is in Java. In the following example,

(:source lang=cpp:)
int *myGrades = new int[ NUM_GRADES ];
myGrades[ 0 ] = 85;
myGrades[ 1 ] = 90;
myGrades[ 2 ] = 87;
myGrades[ 3 ] = 65;
myGrades[ 4 ] = 91;

we create a five element int dynamic array as shown below

Note that the array itself is dynamic but it does not contain dynamic elements. You will also notice the use of the pointer notation to specify the array pointer variable.

Deleteing Dynamic Arrays. As with any dynamic variable, you are responsible for deallocation when the dynamic memory is no longer needed. The delete command is also used to delete a dynamic array.

(:source lang=cpp:)
delete myGrades;

You can only delete an array which was created using the new operator. Static arrays can not be delete since they are not dynamic and their pointer variables are constant.


<< Arrays | Table of Contents | Multi Dimensional Arrays >>

Print - Changes - Search
Last modified: April 30, 2007, at 10:46 PM.