Lecture Preview: Memory and Pointers

(Suggested book reading: Programming Abstractions in C++, 11.2, 12.1, 12.5)

Today we will learn about a C++ feature called pointers. A pointer stores the memory address of another value. Get the address of an existing value by preceding it with & . Declare a pointer by following a data type with * . You can also follow a pointer to the memory address at which it points by preceding its name with * . Here is a brief example:

int x = 42;
int* p = &x;
cout <<  p << endl;   // 0x7f8e20
cout << *p << endl;   // 42
*p = 99;
cout << x << endl;    // 99

We use the special value nullptr to indicate the pointer variable does not hold a valid memory location. nullptr is a special value in C++ representing a pointer to nothing; it indicates the lack of any further data.

This document and its content are copyright © Marty Stepp and Julie Zelenski, 2019. All rights reserved. Any redistribution, reproduction, transmission, or storage of part or all of the contents in any form is prohibited without the authors' expressed written permission.