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
Figure 4.9. Pointers store addresses.
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.
Note
In C++, the combination int * is a compound type, pointer-to-int.
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.
Listing 4.16. init_ptr.cpp
// init_ptr.cpp -- initialize a pointer
#include
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.)
Pointer Danger
Danger awaits those who incautiously use pointers. One extremely important point is that when you create a pointer in C++, the computer allocates memory to hold an address, but it does not allocate memory to hold the data to which the address points. Creating space for the data involves a separate step. Omitting that step, as in the following, is an invitation to disaster:
long * fellow; // create a pointer-to-long
*fellow = 223323; // place a value in never-never land
Sure, fellow is a pointer. But where does it point? The code failed to assign an address to fellow. So where is the value 223323 placed? We can’t say. Because fellow wasn’t initialized, it could have any value. Whatever that value is, the program interprets it as the address at which to store 223323. If fellow happens to have the value 1200, then the computer attempts to place the data at address 1200, even if that happens to be an address in the middle of your program code. Chances are that wherever fellow points, that is not where you want to put the number 223323. This kind of error can produce some of the most insidious and hard-to-trace bugs.
Caution
Pointer Golden Rule:
Pointers and Numbers