User Tools

Site Tools


cplusplus:pointers_1_slides

This is an old revision of the document!


View page as slide show

Pointers 1 slides

Pointer fundamentals, syntax, and operations.1)
Mithat Konar
March 28, 2019

Introduction

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

Pointer variables

  • Variables are stored in blocks of computer memory.
  • base address: the memory address of the first byte of this block.
  • 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 ints occupy 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

Changing the value of countPtr to point to num:

Pointer syntax basics

Declaration

  • * character is used to indicate pointer variables in declarations and function parameter lists.
  • Type of data being pointed to must also be indicated.
int *myPtr;     // declare a pointer to an int
bool *yourPtr;  // declare a pointer to a bool
  • Location of the * character is flexible:
int *myPtr;
int* myPtr;
int * myPtr;

Declaration

  • You can declare more than one pointer variable at a time:
int *myPtr, *anotherOne;   // declare two pointers to an int
  • But be careful::
int* myPtr, anotherOne;   // a pointer and an int

Address operator

  • To set the value of a pointer, you need the address of something.
  • address operator & 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.

Assignment

  • Declare an integer variable y and a pointer variable myPtr set to store the address of (“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

Assignment

  • Declare an integer variable y and a pointer variable myPtr set to store the address of (“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
Variable name Memory location Memory contents
y520005
52003
myPtr6300252000
63009

Assignment

  • Chaning the value of pointer variables:
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

  • Local pointer variables in C++ are not automatically initialized.
  • Uninitialized pointers point to arbitrary memory. Very dangerous.
  • You can initialize when declared:
int y = 5;
int *myPtr = &y;  // myPtr gets address of y

nullptr/NULL pointers

  • You can set a pointer to a value that indicates that it is pointing to nothing: nullptr (C++11) or NULL (earlier versions).
  • Otherwise uninitialized pointers should be set to nullptr (or NULL).
int *yourPtr = nullptr; // yourPtr points to nothing
int y = 5;
yourPtr = &y;           // yourPtr gets address of y
  • nullptr and NULL have an integer value of 0, so sometimes programmers use 0 or '\0'.

Pointer operators

  • address operator & (above).
  • indirection or dereferencing operator * (below).

Indirection/dereferencing operator

  • indirection or dereferencing operator, *, accesses the value of what its operand points to.
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

  • Dereferencing 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;

Indirection/dereferencing operator

  • * and & operators complement each other.
  • The following expressions all evaluate as true:
*&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.
cplusplus/pointers_1_slides.1554066227.txt.gz · Last modified: 2019/03/31 21:03 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki