Pointers to a single object

You can allocate a single object of any given type by just leaving the square brackets off the new statement. For example, to create a single int object and a pointer, p, that points to it, we could write:
int* p = new int;
To delete the int that p points to, you use the usual delete statement, except without the square brackets. So to delete the int that p points to, you would write:
delete p;

Dereference

Now, it turns out that you can treat p as an array containing just one int, in the sense that p[0] does indeed give you the int that p points to. However, when we have a pointer to a single object, we usually use an asterisk (*) to get the object from the pointer. This is called dereferencing the pointer. The syntax is
*p
... which evaluates to the object p points to.

Note:

*p is essentially the same as p[0].

So, for example, to allocate a single int using new, and then to assign that int a value we might do something like this:


Step 1: int* p;
        int x = 2;

Step 2: p = new int;

Step 3: (*p) = x + 1;
So in this example, p is a pointer to an int, while *p is the actual int that p points to.

Another example: accessing struct objects

Now, when the objects you allocate are classes, the* for dereference can get a little ugly. For example, consider our ever popular struct point:

struct point
{
  double x, y;
};
We allocate a single object of type point and initialize its x and y coordinates to zero with the following code:

point* ptr = new point;
(*ptr).x = 0;
(*ptr).y = 0;
This syntax is ugly. As we already know, we usally use the following notation for the same task.

point* ptr = new point;
ptr->x = 0;
ptr->y = 0;

Note:
p->member
is essentially the same as
(*p).member or p[0].member

Address-of

A pointer can also point to a local variable (i.e. one that was declared rather than allocated with new).In order to get a pointer to an object from the object itself, you use the & operator. The expression &name evaluates to a pointer to the variable name. The & is called the "address-of operator". So, for example:

int* p;
int x;
p = &x;     // the value p becomes the address of x
(*p) = 3;   //* p is the same as x, since p points to x.
cout << x << endl;
will print out "3". The second line sets p to point to the int x. This third line sets the "int pointed to by p" (which is x) to 3.

Heads up! Different meanings for the same symbols

Just as a heads up, now the* and & symbols mean different things in different contexts.
operator in a variable declaration in an expression
* declaring a pointer type
int* p;
dereference
(*p) = 3;
& declaring a reference type
void f(int& x);
address-of
int* p;
p = &x;

operator*

The symbol * has three meanings: multiplication, pointer-declaration, and dereference. How do you know which? Context!

int* p;            // this is a declaration
                   // * means pointer declaration

*p = 7;            // *p is an expression;  the*  is used as a unary rather than binary operator
                   // * means dereference

cout << 3*7;       // 3*7 is an expression, and the*  is used as a binary operator
                   // *  means multiplication

operator &

The symbol & has two meanings: pass-by-reference and address-of (or pointer-to).How do you know which? Context!

void foo(int& x);      // this is a declaration
                       // & means pass-by-reference

int* p = &x;           // &x is an expression
                       // & means address-of (or pointer-to)

Rule of thumb

It's also important to think about what these operators have to do with types. Generally,

Quick check:

Drag your mouse for the answers.

char* S;
double** A;
int n;
expressiontype
*Schar
&Schar**
*Adouble*
&Adouble***
*nerror!
&nint*

Why would I ever want a pointer to address-of and dereference?

One big reason is that you will use C (instead of C++) in IC221 Systems Programming. The C programming language does not have pass-by-reference like C++ does, so in C programs if we want the same effect we have to call a function with a pointer to the object we want the function to modify.

incHour() in C

Recall the function incHour() in Class22:

As you all should know, the output of the program will be as follows:

Current hour: 3
Current hour: 4
This is a typical usage of pass-by-reference.

#include <iostream>
void incHour(int& h);

int main()
{
  int h = 3;
  cout << "Current hour: " << h << endl;
  incHour(h);
  cout << "Current hour: " << h << endl;
  return 0;
}

void incHour(int& h)
{
  h++;
}
In order to write a program without using pass-by-reference (like in C), we need to use pass-by-pointer as follows:

Notice what changed:


#include <iostream>
void incHour(int* hp);      // changed here

int main()
{
  int h = 3;
  cout << "Current hour: " << h << endl;
  incHour(&h);              // changed here
  cout << "Current hour: " << h << endl;
  return 0;
}

void incHour(int* hp)       // changed here
{
  (*hp)++;                  // changed here
}

Famous swap, revisited

Consider the code on the left. Write the code on the right without using pass-by-reference so that they work the same (drag your mouse for the answer).

#include <iostream>

void swap(int& , int& );

int main()
{
  int x = 2, y = 3;
  swap(x,y);
  cout << x << " " << y << endl;
  return 0;
}

void swap(int& a, int& b)
{
  int t = a;
  a = b;
  b = t;
}
#include <iostream>

void swap(int* , int* );

int main()
{
  int x = 2, y = 3;
  swap(&x, &y);
  cout << x << " " << y << endl;
  return 0;
}

void swap(int* ap, int* bp)
{ 
  int t = *ap;
  *ap = *bp;
  *bp = t;
}