View page as slide show

Pointers 1 slides

Pointer fundamentals, syntax, and operations.1)
Mithat Konar
October 19, 2021

Introduction

Pointer variables

Two ways to think about pointers

Pointer hardware model

Variable name Memory location Memory contents across all four bytes (32 bits)
count5800000000000000000000000000000000111
(i.e. 7 in decimal)
58001
58002
58003

Pointer hardware model

Variable name Memory location Memory contents across all 8 bytes (64 bits)
countPtr6400200000000000000000000000000000000
00000000000000001110001010010000

(58000 in decimal)
64003
64009

Pointer visual model

Given the count variable used above: a pointer variable countPtr pointing to count would be represented as:

The value of countPtr is the box the arrow points to (i.e., the integer count).

Pointer visual model

Changing the value of countPtr to point to num:

Pointer syntax basics

Pointer declaration

int *myPtr;     // declare a pointer to an int
bool *yourPtr;  // declare a pointer to a bool
int *myPtr;
int* myPtr;
int * myPtr;

Pointer declaration

int *myPtr, *anotherOne;   // declare two pointers to an int
int* myPtr, anotherOne;   // a pointer and an int

Address operator

int num = 42;
cout << num << endl;   // prints value held in variable num
cout << &num << endl ; // prints the base address of variable num

Pointer assignment

int y = 5;   // declare an integer variable y
int *myPtr;  // declare a pointer to int
myPtr = &y;  // myPtr gets address of ("points to") y

Pointer assignment

int y = 5;   // declare an integer variable y
int *myPtr;  // declare a pointer to int
myPtr = &y;  // myPtr gets address of ("points to") y
Variable name Memory location Memory contents
y520005
52003
myPtr6300252000
63009

Pointer assignment

double z = 3.33;
double x = 42.0;
double *myPtr;
 
myPtr = &z;   // myPtr gets address of z
myPtr = &x;   // myPtr now has address of x

Pinter initialization

int y = 5;
int *myPtr = &y;  // myPtr gets address of y

nullptr/NULL pointers

int *yourPtr = nullptr; // yourPtr points to nothing
int y = 5;
yourPtr = &y;           // yourPtr gets address of y

Operators for pointers

Indirection/dereferencing operator

int y = -1;       // declare y and initialize its value
int *myPtr = &y;  // declare pointer and set it to point to y
 
cout << *myPtr;   // defererence myPtr (prints -1)

Indirection/dereferencing operator

int y = -1;       // declare y and initialize its value
int *myPtr = &y;  // declare pointer and set it to point to y
 
*myPtr = 7;       // change value in y to 7
cout << y;        // prints 7
the_thing_at_the_end_of_myPtr = 7;

Indirection/dereferencing operator

*&y == y
&*myPtr == myPtr
*&myPtr == myPtr

Example

pointer-example.cpp
/** Demonstrate basic pointer usage. */
#include <iostream>
using namespace std;
int main()
{
    int a;                  // a is an integer
    int *aPtr = nullptr;    // aPtr is a pointer to an integer
 
    a = 7;      // give a a value
    aPtr = &a;  // set aPtr to the address of a
 
    cout << "The value of a is:     " << a << endl
         << "The address of a is:   " << &a << endl
         << "The value of aPtr is:  " << aPtr << endl;
    cout << endl;
 
    cout << "The value of a is:     " << a << endl
         << "The value of *aPtr is: " << *aPtr << endl;
    cout << endl;
 
    cout << "Showing that * and & are inverses of each other:"
         << endl
         << "&*aPtr = " << &*aPtr << endl
         << "*&aPtr = " << *&aPtr << endl;
 
    return 0;
}
1)
Portions loosely adapted from: Deitel, Harvey M., and Paul J. Deitel. “Pointers and Strings.” In C++: How to Program. 3 ed. Upper Saddle River, NJ: Prentice Hall, 2001. 304-388.