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;
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.
*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:
|
p
is a pointer to an
int
, while *p
is the actual int that
p
points to.
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;
p->member
(*p).member
or p[0].member
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.
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;
|
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
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)
For example, if x
is of type int
, then &x
is of type int*
;
if p
is of type int*
, then &p
is of type int**
.
For example, if p
is of type int*
, then *p
is of type int
;
if pp
is of type int**
, then *pp
is of type int*
;
|
|
incHour()
in CincHour()
in Class22:
#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:
#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
}
|
#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; } |