|
A memory address is called a pointer because you can think of it as pointing to a specific spot in memory. From a machine language perspective, a pointer is the same as a long integer (32 bits on a 32-bit machine, 64 bits on a 64-bit machine.) A program can treat a pointer as information in the same way that it treats an integer as information.
But C++ treats pointers a little differently from the way they are treated in machine language. Each pointer p has a type that tells the type of value that is in the memory that p points to. To write the type of a pointer, write an asterisk after another type. For example,
A value of type int* is a pointer to a location in memory that holds a value of type int. If pointer variable p has type int* and holds address 1000, then the memory holds an integer in addresses 1000-1003.
An asterisk is called a star, and type int* is pronounced "int star."
A value of type double* is a pointer to a location in memory that holds a value of type double. If pointer q has type double* and its value is 2000, then the memory holds a value of type double in addresses 2000-2007.
A value of type char* is a pointer to a location in memory that holds a value of type char. So it refers to a single byte.
A value of type char** is a pointer to a location in memory that holds a value of type char*. That is, it points to another pointer. You can add a * to any type, including a pointer type.
Declare one pointer variable just like you would any other variable: write the variable's type followed by its name. For example,
int* q;
declares a variable called q of type int*.
No initial value is stored into q automatically;
q starts out holding a junk pointer, and you need
to store a good pointer into q before using it.
If you want to declare two or more pointer variables
in one declaration, you need to be aware of
a peculiarity of C++: Each variable that is a pointer needs to have
a * in front of it. Statement
char *r, *s;
declares variables r and s, each of type char*. Statement
char *r, s;
declares variable r of type char* and s of type char.
It does not matter where you put spaces. Statement
char* r, s;
still makes s have type char, not char*.
Declaration
int a, *b, **c;
says that a has type int,
b has type int* and c has type int**.
|
If x is a variable then &x is the memory address where x is stored. So int v; int* p = &v;makes variable p hold the address of variable v. Observe that, for any type T:
For example, if x has type int then &x has type int*. |
*p
If p is a pointer then *p is the variable
to which p points. For example,
int v = 1; int* p = &v; *p = 2;ends with v = 2, since *p is the same a v, as shown in the following diagram. ![]() For any type T:
For example, pointer p declared above has type int*. So *p has type int. |
q = p;
If you set pointer variable q equal to pointer variable p,
then you are making q hold the same memory address as p.
So q and p point to the same place, as in the following
diagram.![]() It does not make q point to p. |
*q = *p;
Remember that *p is the variable to which p points.
Assignment *q = *p sets the variable
to which q points
equal to the variable to which p points. That is
not at all the same as assignment q = p.
For example, after
int x = 1, y = 2; int* p = &x int* q = &y; *q = *p;variable y holds 1, since *p is the same as x and *q is the same as y, as illustrated: ![]() |
Operators & and * are opposites of one another.
If x has type T, then &x has type T*. So
Prefix operator & adds a * to the type of a variable. |
If p has type T*, then *x has type T. So
Prefix operator * removes a * from the type of a variable. |
In fact, *&x is always the same as x. Draw a pointer diagram and work that fact out from your diagram.
Notice that you write * after a type but before a variable. Here is a way to remember that. In declaration
int * p;
the star comes between type int and variable p. That is,
star comes after the type and before the variable.
Recall that, for any type T, declaration
T x;declares variable x to have type T. So declaration
int* p;
declares variable p to have type int*. Now, let's
move the *:
int *p;
That appears to be saying that *p has type int.
And it does. C++ is free-form,
and whether you write a space before or after * makes no difference
in meaning.
Saying that p has type T* is equivalent saying that *p has type T. |
Suppose that w is a variable of type long. Write a statement that creates a variable q of type long* and makes q hold the address of variable w. Answer
int r = 0; long* p = &r;allowed? Answer
long r = 0; long* p = r;allowed? Answer
Suppose that p is a pointer of type int*. Write a statement that stores 25 into the variable to which p points. Answer
Suppose that variable p has type Widget*. What is the type of expression *p? Answer
Suppose that variable w has type Widget. What is the type of expression &w? Answer
|