Let’s examine the process of declaring pointers. A computer needs to keep track of the type of value to which a pointer refers. For example, the address of a char
typically looks the same as the address of a double
, but char
and double
use different numbers of bytes and different internal formats for storing values. Therefore, a pointer declaration must specify what type of data to which the pointer points.
For example, the preceding example has this declaration:
int * p_updates;
This states that the combination * p_updates
is type int
. Because you use the *
operator by applying it to a pointer, the p_updates
variable itself must be a pointer. We say that p_updates
points to type int
. We also say that the type for p_updates
is pointer-to-int
or, more concisely, int *
. To repeat: p_updates
is a pointer (an address), and *p_updates
is an int
and not a pointer (see Figure 4.9).
Incidentally, the use of spaces around the *
operator are optional. Traditionally, C programmers have used this form:
int *ptr;
This accentuates the idea that the combination *ptr
is a type int
value. Many C++ programmers, on the other hand, use this form:
int* ptr;
This emphasizes the idea that int*
is a type, pointer-to-int
. Where you put the spaces makes no difference to the compiler. You could even do this:
int*ptr;
Be aware, however, that the following declaration creates one pointer (p1
) and one ordinary int
(p2
):
int* p1, p2;
You need an *
for each pointer variable name.
You use the same syntax to declare pointers to other types:
double * tax_ptr; // tax_ptr points to type double
char * str; // str points to type char
Because you declare tax_ptr
as a pointer-to-double
, the compiler knows that *tax_ptr
is a type double
value. That is, it knows that *tax_ptr
represents a number stored in floating-point format that occupies (on most systems) 8 bytes. A pointer variable is never simply a pointer. It is always a pointer to a specific type. tax_ptr
is type pointer-to-double
(or type double *
), and str
is type pointer-to-char
(or char *
). Although both are pointers, they are pointers of two different types. Like arrays, pointers are based on other types.
Note that whereas tax_ptr
and str
point to data types of two different sizes, the two variables tax_ptr
and str
themselves are typically the same size. That is, the address of a char
is the same size as the address of a double
, much as 1016 might be the street address for a department store, whereas 1024 could be the street address of a small cottage. The size or value of an address doesn’t really tell you anything about the size or kind of variable or building you find at that address. Usually, addresses require 2 or 4 bytes, depending on the computer system. (Some systems might have larger addresses, and a system can use different address sizes for different types.)
You can use a declaration statement to initialize a pointer. In that case, the pointer, not the pointed-to value, is initialized. That is, the following statements set pt
and not *pt
to the value &higgens
:
int higgens = 5;
int * pt = &higgens;
Listing 4.16 demonstrates how to initialize a pointer to an address.
// init_ptr.cpp -- initialize a pointer
#include <iostream>
int main()
{
using namespace std;
int higgens = 5;
int * pt = &higgens;
cout << "Value of higgens = " << higgens
<< "; Address of higgens = " << &higgens << endl;
cout << "Value of *pt = " << *pt
<< "; Value of pt = " << pt << endl;
return 0;
}
Here is some sample output from the program in Listing 4.16:
Value of higgens = 5; Address of higgens = 0012FED4
Value of *pt = 5; Value of pt = 0012FED4
You can see that the program initializes pt
, not *pt
, to the address of higgens
. (Your system most likely will show different values for the addresses and may display them in a different format.)
3.133.133.233