User Tools

Site Tools


cplusplus:pointers_1_slides

This is an old revision of the document!


View page as slide show

Pointers 1

Pointer fundamentals, syntax, and operations.1)

Introduction

  • Pointers found in many programming languages.
  • Important in C++ because a close relationship between pointers and arrays, C strings, and references.

Address of a variable

  • Variables are stored in blocks of computer memory.
  • The base address of a variable is the memory address corresponding to the starting point of this block.

Pointer variables

  • pointer variable: a variable that stores the base address of some other variable.
  • Usefulness will be seen after understanding how to use them.

Two ways to think about pointers

  • Pointers hardware model
  • Pointer visual model

Pointer hardware model

Assume an int named count storing the value 7 and an int occupies 4 bytes.

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

The address of count is the base address 58000.

Pointer hardware model

A pointer variable set to point to count would store the value 58000.

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

(i.e., 58000 in decimal)
64003
64009

The value of the pointer variable is the base address it stores.

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

If we change the value of countPtr to point to num, the diagram would now look like:

Pointer syntax basics

Declaration

In C++, the * character is used to indicate pointer variables in declarations and function parameter lists. The code fragment below creates a pointer variable named myPtr that can point to a block of memory that stores an int:

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

The location of the * character is flexible: it can be placed before the name of the pointer (as in the above example), immediately after the type, or separated from both the type and the name by whitespace. All the declarations below are equivalent:

int *myPtr;
int* myPtr;
int * myPtr;

You can declare more than one pointer variable at a time:

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

But be careful: the following actually creates one pointer and one int:

int* myPtr, anotherOne;   // a pointer and an int

Pointers can be created to point to any type:

char *yourPtr;  // declare a pointer to a char
double *zerPtr; // declare a pointer to a double
string *hyPtr;  // declare a pointer to a string

Assignment

To set the value of a pointer, you need the address of something. C++ has an address operator, &, that returns the base address of a variable:

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

Most environments show base addresses as hexadecimal numbers by default.

The example below declares an integer variable y that is initialized to 5 and a pointer variable to an int named myPtr that is set to store the address of (i.e., “point to”) y.

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

The result of this code fragment may be diagrammed as follows:

and when run may generate the following memory map:

Variable name Memory location Memory contents
y520005
52001
52002
52003
myPtr6300252000
63003
63004
63005
63006
63007
63008
63009

You can change the value of pointer variables (that's why it's called a variable!):

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

Initialization

Pointer variables in C++ are not automatically initialized. This means that a pointer variable declaration along the lines of

double *myPtr;

leaves the pointer pointing to an arbitrary memory location. This is dangerous because poking your fingers into arbitrary memory locations is a good way to corrupt your program's memory or create other headaches.

Pointer variables can be initialized when declared. It is good programming practice to always initialize pointers so they do not accidentally point to unknown memory locations. The code below initializes the value of the pointer variable in the declaration:

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

nullptr/NULL pointers

You can set a pointer to a special value that indicates that it is pointing to nothing. This special value has the name nullptr (C++11) or NULL.

If the memory location that a pointer variable will point to isn’t known at the time it is declared, then you should initialize it to nullptr or NULL. This will prevent the pointer from accidentally pointing to arbitrary memory locations.

Below is an example of initializing a pointer variable to NULL when declaring it.

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

Note how yourPtr is initialized to nullptr even though we change the pointer immediately afterwards. We did this because it's a good habit to initialize your pointers so they never point to unknown memory locations.

nullptr and NULL have an integer value of 0, so sometimes you will see pointer variables set to 0 instead. Similarly, the character '\0' also has an integer value of 0, so sometimes you will see pointers set to '\0'. All three have the same effect: they make the pointer point to nothing.

Pointer operators

You were introduced to the address operator, &, above. The address operator is one of two fundamental pointer operators. The other is the indirection or dereferencing operator.

Indirection/dereferencing operator

The indirection or dereferencing operator, *, returns the value of what its operand points to. For example, if myPtr points to variable y, *myPtr returns the value stored in y.

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

The * operator can be used to assign a value to a location in memory:

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

You can think of the indirection/dereferencing operator as meaning, “the_thing_at_the_end_of_”, as in:

the_thing_at_the_end_of_myPtr = 7;

The * and & operators complement each other. In other words, the following expressions will all evaluate as true:

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

Example

The example below demonstrates the use pointers, including address and dereferencing operators.

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.
cplusplus/pointers_1_slides.1553787056.txt.gz · Last modified: 2019/03/28 15:30 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki