C++ Primer For Java Programmers


Pointers

<< Text Files | Table of Contents | Dynamic Memory >>
In computer programming, a pointer is a memory address which can be used to indirectly reference data at the given location. In Java, pointers were used to store references to objects created during execution of the program. In the following Java example

(:source lang=java:)
StudentRecord record = new StudentRecord();

a StudentRecord object is created and its address is assigned to the record variable.

To use the object we must access it indirectly through the record pointer variable.

(:source lang=java:)
sys.out.println( record.idNum + record.gpa );

In C++, the use of pointers is optional for most data types including objects and structures but as we will later see, they are required for arrays. A varible created as follows

(:source lang=cpp:)
int x;

allows for direct access to the data stored in the variable.

While Java provides direct access variables for the simple types such as int and double, pointers are required for objecst. In the C++ language, both direct and indirect access is possible for all data types including objects and structures.

Creating Pointers

In C++, pointers can be created for any data type, but once created, they can only store addresses for data of the given type. To declare a pointer in C++, we use the star ('*') notation. In the following code segment,

(:source lang=cpp:)
int x = 12;
int y = 50;
int *p = NULL;
int *q;

we create two int variables (x and y) and two int pointer variables (p and q). As with other variables, C++ pointers are not initialized by default. In our example, we set one pointer variable to the null pointer using the NULL value. If no initial value is provided, the pointer contains some memory address, though invalid. In the diagram below, the current state of the four varialbles is illustrated.

C++ allows for the creation of void pointers which can store an address to any data type.

(:source lang=cpp:)
void *vptr;
  • A pointer of any type can be assigned to a void pointer
(:source lang=cpp:)
vptr = q;
  • A void can not be directly assigned to a pointer of a given type. Instead, the void pointer must be typecast to a pointer of the appropriate type
(:source lang=cpp:)
p = (int *) vptr;

Pointer Assignments

Any address can be assigned to a pointer variable so long as that address refers to a variable of the same type as the pointer variable. It is not unusual to assign the address of a varaible to a pointer, we use the address-of operator (&). For example,

(:source lang=cpp:)
p = &x;

results in the address of x being assigned to pointer p and thus p points to x.

Dereferencing Pointers. The value of x can now be changed indirectly by dereferencing pointer p

(:source lang=cpp:)
*p = 8;

which results in the following changes

Pointer-to-Pointer Assignment. Two pointers of the same type can be assigned to each other. For example, we can copy the contents of pointer p to pointer q since both are pointers to integers.

(:source lang=cpp:)
q = p;

The result of this assignment is such that both p and q both point to the same variable as shown below. Thus, dereferencing pointer q will affect variable x just as if you were to use pointer p.

Null Pointers. You should rmember from Java that a null pointer is one which contains no address and thus points to nothing. In C++, the value NULL is used to represent a null pointer. If we set execute

(:source lang=cpp:)
p = NULL;

the results are illustrated below

As in Java, attempting to dereference a null pointer results in a run-time error. Though, in C++ it can be more difficult to locate the actual statement in which the error occurred.

Pointer Comparisons

Pointers can be used in logical expressions to determine the equality of two pointers. To determine if two pointers point to the same memory location simply compare the two pointer variables

(:source lang=cpp:)
if( p == q ) 
  printf( "Both p and q point to the same location.\n" );
else
  printf( "p and q point to different locations.\n" );
}

To determine if a pointer is null, compare the pointer variable to the NULL pointer value

(:source lang=cpp:)
if( p == NULL ) 
  printf( "p is null.\n" );
else
  printf( "p is not null.\n" );

Function Parameters

(:source lang=cpp:)
void swap( int *a, int *b )
{
  int t;
  t = *a;
  *a = *b;
  *b = t;
}

Double Indirection

(:source lang=cpp:)
int x = 5;
int *p = &x;
int *w = &p;

printf( "%d\n", **w );

Summary


<< Text Files | Table of Contents | Dynamic Memory >>

Print - Changes - Search
Last modified: April 30, 2007, at 09:57 PM.