Please send questions to
st10@humboldt.edu .
a pointer is (Savitch, p.698):
"the memory address of a variable"
* it is a special kind of value
* in C++, part of a pointer's type is not JUST that
it's a pointer -- it is a pointer TO some type;
* so -- in C++ -- how can I declare something
to be of this special pointer type?
...and how can I then use it?
double *num_ptr; // num_ptr's type is pointer to a
// double
// num_ptr can now contain the
// address of a double variable/
// value somewhere in memory;
int *quant_ptr; // quant_ptr's type is pointer to
// an int
// quant_ptr can now contain
// the address of an int somewhere
// in memory
boa *boa_ptr; // boa_ptr's type is pointer to
// a boa
// boa_ptr can now contain the
// address of a boa instance
// somewhere in memory
* If I want to indicate that currently,
a pointer is pointing to nothing,
by STRONG convention you should set it to
NULL
* (NULL is a named constant set in
the cstdlib)
boa_ptr = NULL;
num_ptr = NULL;
quant_ptr = NULL;
* [this is now a class coding standard --
pointers that aren't currently pointing
anywhere should be set to NULL]
* But, we probably want our pointers to actually
contain addresses at some point (to actually
point TO something) --
ONE (of several) ways:
* & can be a PREFIX operator,
and when it is, it gets you the ADDRESS
of the variable/lvalue it is in front of;
double my_salary;
// I can make num_ptr contain the address of
// my_salary -- make it POINT TO my_salary --
// with the statement:
num_ptr = &my_salary; // num_ptr now contains the
// memory address -- the
// location in memory --
// of value my_salary
int num_widgets;
quant_ptr = &num_widgets; // quant_ptr now contains
// the memory address of
// num_widgets
* if you have a pointer to something --
you are able to access the value WHERE
that pointer is pointing --
that is, you are able to access the value
at the address;
* you use the prefix operator *
to do this;
if I now say
cout << *quant_ptr << endl;
...this will print the int at the address in
quant_ptr;
or, if you prefer, it will print the int
quant_ptr is pointing to;
here, then, it will print the value of num_widgets
* see ptr_play1.cpp to see the beginning of
a working pointer example;