Let’s now assign a pointer a “real” value, the address of another variable or constant. To do so, you need to access the address of the variable or constant before you can assign that address to the pointer. You use the address operator, covered in Chapter 3, to accomplish this task. The following program shows how to use the address operator to assign the address of a variable to a pointer. This program also demonstrates that the value of a pointer is the same as the address to which the pointer points. #include <iostream>> using namespace std; int main () The output on my computer (the following addresses likely will be different on yours) is The address of x using &num is 0012FED4
The primary use of a pointer is to access and, if appropriate, change the value of the variable that the pointer is pointing to. In the following program, the value of the integer variable num is changed twice. #include <iostream>> using namespace std; int main () The resulting output is The value of num is 5 The first change should be familiar, by the direct assignment of a value to num, such as num=10. However, the second change is accomplished a new way, using the indirection operator: *iPtr = 15; The indirection operator is an asterisk, the same asterisk that you used to declare the pointer or to perform multiplication. However, in this statement the asterisk is not being used in a declaration or to perform multiplication, so in this context it is being used as an indirection operator. NOTE: As mentioned earlier in this chapter, this is another example of a symbol having different meanings in the C++ programming language depending on the context in which it was used. The placement of the indirection operator before a pointer is said to dereference the pointer. Indeed, some texts refer to the indirection operator as the dereferencing operator. The value of a dereferenced pointer is not an address, but rather the value at that address—that is, the value of the variable that the pointer points to. For example, in the preceding program, iPtr’s value is the address of num. However, the value of iPtr dereferenced is the value of num. Thus, the following two statements have the same effect, both changing the value of num: num = 25; Similarly, a dereferenced pointer can be used in arithmetic expressions the same as the variable to which it points. Thus, the following two statements have the same effect: num *= 2; In these examples, changing a variable’s value using the indirection operator rather than through a straightforward assignment seems like an unnecessary complication. However, there are instances covered later in this chapter, such as looping through an array using a pointer, or using dynamic memory allocation, in which using the indirection operator is helpful or even necessary. The Pointer as a Variable or a Constant A pointer may be a variable or a constant. Let’s examine both possibilities. Pointer as a Variable The preceding program had the pointer pointing to one integer variable. However, a pointer variable, being a variable, can point to different variables at different times in the program. In the following program, the value of the pointer is changed to point to two different integer variables. #include <iostream>> using namespace std; int main () The resulting output is therefore: The value of num1 is 5
blog comments powered by Disqus |
|
|
|
|
|
|
|