The program in Fig. 8.4 demonstrates the &
and *
pointer operators. Memory locations are output by <<
in this example as hexadecimal (i.e., base-16) integers. (See Appendix D, Number Systems, for more information on hexadecimal integers.) The memory addresses output by this program are platform dependent, so you may get different results when you run the program. The address of a
(line 11) and the value of aPtr
(line 12) are identical in the output, confirming that the address of a
is indeed assigned to the pointer variable aPtr
.
1 // Fig. 8.4: fig08_04.cpp
2 // Pointer operators & and *.
3 #include <iostream>
4 using namespace std;
5
6 int main()
7 {
8 int a = 7; // assigned 7 to a
9 int *aPtr = &a; // initialize aPtr with the address of int variable a
10
11 cout << "The address of a is " << &a
12 << "
The value of aPtr is " << aPtr;
13 cout << "
The value of a is " << a
14 << "
The value of *aPtr is " << *aPtr << endl;
15 } // end main
The address of a is 002DFD80
The value of aPtr is 002DFD80
The value of a is 7
The value of *aPtr is 7
3.139.105.159