CSE 202: Computer Science II, Winter 2018
Pointers

Every named object, subobject of a named object, and function in a C++ program exists at a unique address in memory.

There are a family of types in C++ called pointers. Valid pointer values can be:

The following example demonstrates the manipulation of one variable through a pointer to it:

int x; // Declares x with type "int" int* p; // Declares p with type "pointer to int" p = &x; // Assigns the address of x to p *p = 7; // Dereference p and assign 7 std::cout << *p << std::endl; std::cout << x << std::endl;

The symbol *, when used in a declaration, is used to declare a pointer. When used on a pointer value within an expression, it is known as the indirection operator. The indirection operator gives you access to the object that the pointer refers to. From the example, the expression *p has the same value as x and the expression *p = 7 has the same result as x = 7.

& is the address-of operator, when used on an object or function the result is a pointer to that entity.

Pointers to array elements

An array exists as a contiguous sequence of elements in memory. The address of an array is the same as the address of it's first element.

double a[3]; double* p = &a[0]; *p = 5.0; ++p; *p = 3.1; ++p; *p = -4.2; std::cout << a[0] << ' ' << a[1] << ' ' << a[2] << std::endl;

In the above example, the pointer p is initialized to the address of the first element of a. Because the elements exist sequentially in memory, incrementing p assigns the address of the next element to p.

You can iterate through an array using pointers instead of an index. In this example, the array of n   double, a, gets displayed using pointers for iteration:

double* first = &a[0]; double* last = first + n; while (first != last) { std::cout << *first++ << ' '; }
Null pointers

A pointer with the value 0 is known as a null pointer. Null pointers do not point to any object or function and cannot be dereferenced.

Null pointers are used to indicate the absence of an object or an error condition.

A null pointer can be obtained by casting the integer literal 0 to a pointer type or through value initialization. For example, the expression (float*)0 results in a null pointer to float. Also assigning the integer literal 0 to any pointer variable implicitly converts it to a pointer value.

Void pointers

Pointers of type void* or const void* are known as pointers to void or void pointers. A void pointer points to an object(s) of unknown type.

void pointers cannot be dereferenced but a pointer to any other type can be implicitly converted to a void pointer and a void pointer can be explicitly converted to a pointer to any other type.

int x; int* p1 = &x; void* p2 = p1; // p1 implicitly converted to void* int* p3 = (int*)p2; // p2 explicitly converted to int*
Array-to-pointer decay

Array-to-pointer decay refers to the conditions when an array within an expression or declaration gets implicitly converted to a pointer to it's first element.

This happens when:

In the first case, a function such as void f(int a[]) {} actually gets replaced with void f(int* a) {} during compilation.

An array argument in a function call gets replaced with a pointer to that array's first element. So an expression like f(a), where f is a function with an array parameter and a is an array, gets replaced with f(&a[0]);.

In the second case, an expression such as p = a where p is a pointer and a is an array, gets replaced with p = &a[0].

The subscript operator still works with pointers. So pointers can still be treated like arrays.

Pointer constness
Type Meaning
const T* Pointer to constant T
T const* Pointer to constant T
T* const Constant pointer to T
const T* const Constant pointer to constant T