View page as slide show

Pointers 2 slides

Pointers and comparisons, functions, and qualifiers.1)
Mithat Konar
March 28, 2019

Comparing pointers

double x = 2.2;
double y = 4.8;
double *aPtr = &x;
double *bPtr = &y;
 
if (aPtr == bPtr) ...   // compare addresses
if (*aPtr == *bPtr) ... // compare contents of addresses, i.e. same as
if (x == y) ...

Pointers as function parameters

Example

function-with-pointer.cpp
/** Demonstrates how to pass pointers to functions. */
#include <iostream>
using namespace std;
 
void printPtr(int *);   // prototype
 
int main()
{
    int number = 5;
    int *myPtr = &number;
 
    printPtr(myPtr);     // pass in a pointer
    printPtr(&number);   // pass in an address
 
    return 0;
}
 
/**
 * Simple function showing how to pass pointers to functions.
 * Print the value of nPtr and the value of dereferenced nPtr.
 */
void printPtr(int *nPtr)
{
    cout << "The value of the pointer is " << nPtr << endl
         << "The value of the thing pointed to by the pointer is "<< *nPtr << endl;
}

Calling functions by reference

Example

cube-by-reference.cpp
/** Cube a variable using pass by reference with a pointer. */
#include <iostream>
using namespace std;
void cubeByReference(int *);   // prototype
 
int main()
{
    int number = 5;
    cout << "The original value of number is " << number << endl;
    cubeByReference(&number);
    cout << "The new value of number is " << number << endl;
    return 0;
}
 
/** Cube the integer pointed to by nPtr. */
void cubeByReference(int *nPtr)
{
    // *nptr'' is an alias for the variable passed in.
    *nPtr = (*nPtr) * (*nPtr) * (*nPtr); // parenthesis for readability
}

The const qualifier

The const qualifier

Constant pointers

int *const myPtr = &x;  // constant pointer to a (non-constant) int
                        // myPtr will always point to x

Example

const-ptr-demo.cpp
/** Attempting to modify a constant pointer to non-constant data. */
#include <iostream>
using namespace std;
int main()
{
    int x = 2, y = 5;
    int *const myPtr = &x;  // constant pointer to a (non-constant) int
                             // The data pointed to by myPtr can be 
                             // modified through myPtr, but myPtr must
                             // always point to the same memory location.
 
    *myPtr = 99;   // change value stored in x to 99
    myPtr = &y;    // syntax error!! can't point to a different int
 
    cout << *myPtr << endl;
 
    return 0;
}

Pointer to constant data

int x = 2, y = 5;
const int *myPtr = &x;  // non-constant pointer to a constant int
 
*myPtr = 99;   // syntax error! can't modify what it's pointing to
myPtr = &y;    // no problem, myPtr now points to y

Constant pointer to constant data

int x = 2, y = 5;
const int *const myPtr = &x;// constant pointer to a constant int
 
*myPtr = 99;   // syntax error!
myPtr = &y;    // syntax error!

How to remember

/* const modifies myPtr, meaning the value of myPtr is constant. */
int *const myPtr = &x;
/* const modifies int, meaning the value of the int is constant. */
const int *myPtr = &x;
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.