1.15.1.  The Unary Operators & and *

[ fromfile: pointers.xml id: refderefoperators ]

[Warning]Warning

The symbol * is used in two different ways in connection with pointers:

  • It can serve as a type modifier, in a pointer variable definition.

  • It can be used as the dereference operator.

Example 1.27. src/pointers/pointerdemo/pointerdemo.cpp

#include <QTextStream>

int main() {
    QTextStream cout(stdout);
    int x = 4;
    int* px = 0 ;            1
    px = &x;
    cout << "x = " << x
         << " *px = " << *px 2
         << " px = " << px
         << " &px = " << &px << endl;
    x = x + 1;
    cout << "x = " << x
         << " *px = " << *px
         << " px = " << px << endl;
    *px = *px + 1;
    cout << "x = " << x
         << " *px = " << *px
         << " px = " << px << endl;
    return 0;
}


Output:

OOP> ./pointerdemo
x = 4 *px = 4 px = 0xbffff514 &px = 0xbffff510
x = 5 *px = 5 px = 0xbffff514
x = 6 *px = 6 px = 0xbffff514
OOP>


1

Type modifier

2

Unary dereference operator


Figure 1.4.  Pointer Demo

Pointer Demo