Every variable has an address. Even without knowing the specific address of a given Variable, you can store that address in a pointer. For example, suppose that the variable howOld is an integer. To declare a pointer called pAge to hold its address, you write the following statement:
int *pAge = NULL;
This declares pAge to be a pointer to int. That is, pAge is declared to hold the address of an int. Pointers can have any name that is legal for other variables. Here we follow the convention of naming all pointers with an initial p and a second letter capitalized, as in pAge.
Note that pAge is a variable like any other variable. When you declare an integer variable, it is set up to hold an integer. When you declare a pointer variable like pAge, it is set up to hold an address. A pointer is just a special type of variable that holds the address of an object in memory; in this case, pAge is holding the address of an integer variable. You declare the type of variable you want the pointer to point to. This tells the compiler how to treat the memory at the location the pointer points to. The pointer itself contains an address.
What actually happens inside |
In this example, pAge is initialized to NULL. A pointer whose value is NULL is called a null pointer. All pointers, when they are created, should be initialized to something. If you don’t know what you want to assign to the pointer, assign NULL. A pointer that is not initialized is called a wild pointer. Wild pointers are dangerous. If you initialize the pointer to 0 or NULL, you must specifically assign the address of howOld to pAge. Here’s code that shows how to do that:
The result should be the same as if you initialized it to NULL, but technically 0 is an integer constant, and NULL is an address constant of 0. The next version of C, C++, has a new nullptr constant that represents a null pointer. When your C++ compiler supports this new version, use nullptr instead of 0 or NULL.
int howOld = 50;
int *pAge = 0;
pAge = &howOld;
// make a variable
// make a pointer
// put howOld’s address in pAge
The first line creates a variable—howOld, whose type is unsigned short int—and initializes it with the value 50. The second line declares pAge to be a pointer to type unsigned short int and initializes the address to 0. You know that pAge is a pointer because of the asterisk (*) after the variable type and before the variable name. The third and final line assigns the address of howOld to the pointer pAge. You can tell that the address of howOld is being assigned to the pointer because of the address of operator &. If the address of operator was not used, the value of howOld would be assigned instead of its address. That value might be a valid address somewhere in memory, but that would be entirely a coincidence. Assigning a nonpointer to a pointer variable is a common error. Fortunately, the compiler will detect this and fail with an “invalid conversion” error. At this point, pAge has as its value the address of howOld. howOld, in turn, has the value 50. So, it represents the basic idea about storing the address in a pointer.
0 comments:
Post a Comment