📄 ch08.htm
字号:
yourAge = *pAge;</FONT></PRE><P>The indirection operator (<TT>*</TT>) in front of the variable <TT>pAge</TT> means"the value stored at." This assignment says, "Take the value storedat the address in <TT>pAge</TT> and assign it to <TT>yourAge</TT>."<BLOCKQUOTE> <P><HR><FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>The indirection operator (<TT>*</TT>) is used in two distinct ways with pointers: declaration and dereference. When a pointer is declared, the star indicates that it is a pointer, not a normal variable. For example,</P> <PRE><FONT COLOR="#0066FF">unsigned short * pAge = 0; // make a pointer to an unsigned short</FONT></PRE></BLOCKQUOTE><PRE><FONT COLOR="#0066FF"></FONT></PRE><BLOCKQUOTE> <P>When the pointer is dereferenced, the indirection operator indicates that the value at the memory location stored in the pointer is to be accessed, rather than the address itself.</P> <PRE><FONT COLOR="#0066FF">*pAge = 5; // assign 5 to the value at pAge</FONT></PRE></BLOCKQUOTE><PRE><FONT COLOR="#0066FF"></FONT></PRE><BLOCKQUOTE> <P>Also note that this same character (<TT>*</TT>) is used as the multiplication operator. The compiler knows which operator to call, based on context. <HR></BLOCKQUOTE><H4 ALIGN="CENTER"><A NAME="Heading11"></A><FONT COLOR="#000077">Pointers, Addresses,and Variables</FONT></H4><P>It is important to distinguish between a pointer, the address that the pointerholds, and the value at the address held by the pointer. This is the source of muchof the confusion about pointers.</P><P>Consider the following code fragment:</P><PRE><FONT COLOR="#0066FF">int theVariable = 5;int * pPointer = &theVariable ;</FONT></PRE><P><TT>theVariable</TT> is declared to be an integer variable initialized with thevalue <TT>5</TT>. <TT>pPointer</TT> is declared to be a pointer to an integer; itis initialized with the address of <TT>theVariable</TT>.<TT> pPointer</TT> is thepointer. The address that <TT>pPointer</TT> holds is the address of <TT>theVariable</TT>.The value at the address that <TT>pPointer</TT> holds is <TT>5</TT>. Figure 8.3 showsa schematic representation of <TT>theVariable</TT> and <TT>pPointer</TT>.<BR><BR><A NAME="Heading12"></A><A HREF="figure3.jpg" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/art/ch08/figure3.jpg"><FONT COLOR="#000077">Figure8.3.</FONT></A><FONT COLOR="#000077"> </FONT><I>A schematic representation of memory.</I><H4 ALIGN="CENTER"><A NAME="Heading13"></A><FONT COLOR="#000077">Manipulating Databy Using Pointers</FONT></H4><P>Once a pointer is assigned the address of a variable, you can use that pointerto access the data in that variable. Listing 8.2 demonstrates how the address ofa local variable is assigned to a pointer and how the pointer manipulates the valuesin that variable.</P><P><A NAME="Heading14"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 8.2. Manipulatingdata by using pointers.</B></FONT></P><PRE><FONT COLOR="#0066FF">1: // Listing 8.2 Using pointers2:3: #include <iostream.h>4:5: typedef unsigned short int USHORT;6: int main()7: {8: USHORT myAge; // a variable9: USHORT * pAge = 0; // a pointer10: myAge = 5;11: cout << "myAge: " << myAge << "\n";12:13: pAge = &myAge; // assign address of myAge to pAge14:15: cout << "*pAge: " << *pAge << "\n\n";16:17: cout << "*pAge = 7\n";18:19: *pAge = 7; // sets myAge to 720:21: cout << "*pAge: " << *pAge << "\n";22: cout << "myAge: " << myAge << "\n\n";23:24:25: cout << "myAge = 9\n";26:27: myAge = 9;28:29: cout << "myAge: " << myAge << "\n";30: cout << "*pAge: " << *pAge << "\n";31:32: return 0;<TT>33: }</TT></FONT><FONT COLOR="#0066FF">Output: myAge: 5*pAge: 5*pAge = 7*pAge: 7myAge: 7myAge = 9myAge: 9*pAge: 9</FONT></PRE><P><FONT COLOR="#000077"><B>Analysis:</B></FONT><B> </B>This program declares twovariables: an <TT>unsigned short</TT>, <TT>myAge</TT>, and a pointer to an <TT>unsignedshort, pAge</TT>. <TT>myAge</TT> is assigned the value <TT>5</TT> on line 10; thisis verified by the printout in line 11.<BR>On line 13, <TT>pAge</TT> is assigned the address of <TT>myAge</TT>. On line 15,<TT>pAge</TT> is dereferenced and printed, showing that the value at the addressthat <TT>pAge</TT> stores is the <TT>5</TT> stored in <TT>myAge</TT>. In line 17,the value <TT>7</TT> is assigned to the variable at the address stored in <TT>pAge</TT>.This sets <TT>myAge</TT> to <TT>7</TT>, and the printouts in lines 21-22 confirmthis.</P><P>In line 27, the value <TT>9</TT> is assigned to the variable <TT>myAge</TT>. Thisvalue is obtained directly in line 29 and indirectly (by dereferencing <TT>pAge</TT>)in line 30.<H4 ALIGN="CENTER"><A NAME="Heading16"></A><FONT COLOR="#000077">Examining the Address</FONT></H4><P>Pointers enable you to manipulate addresses without ever knowing their real value.After today, you'll take it on faith that when you assign the address of a variableto a pointer, it really has the address of that variable as its value. But just thisonce, why not check to make sure? Listing 8.3 illustrates this idea.</P><P><A NAME="Heading17"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 8.3. Findingout what is stored in pointers.</B></FONT></P><PRE><FONT COLOR="#0066FF">1: // Listing 8.3 What is stored in a pointer.2:3: #include <iostream.h>4:5: typedef unsigned short int USHORT;6: int main()7: {8: unsigned short int myAge = 5, yourAge = 10;9: unsigned short int * pAge = &myAge; // a pointer10:11: cout << "myAge:\t" << myAge << "\tyourAge:\t" << yourAge << "\n";12: cout << "&myAge:\t" << &myAge << "\t&yourAge:\t" << &yourAge <<"\n";13:14: cout << "pAge:\t" << pAge << "\n";15: cout << "*pAge:\t" << *pAge << "\n";16:17: pAge = &yourAge; // reassign the pointer18:19: cout << "myAge:\t" << myAge << "\tyourAge:\t" << yourAge << "\n";20: cout << "&myAge:\t" << &myAge << "\t&yourAge:\t" << &yourAge <<"\n";21:22: cout << "pAge:\t" << pAge << "\n";23: cout << "*pAge:\t" << *pAge << "\n";24:25: cout << "&pAge:\t" << &pAge << "\n";26: return 0;<TT>27: }</TT>Output: myAge: 5 yourAge: 10&myAge: 0x355C &yourAge: 0x355EpAge: 0x355C*pAge: 5myAge: 5 yourAge: 10&myAge: 0x355C &yourAge: 0x355EpAge: 0x355E*pAge: 10&pAge: 0x355A</FONT></PRE><P>(Your output may look different.)</P><P><FONT COLOR="#000077"><B>Analysis:</B></FONT><B> </B>In line 8, <TT>myAge</TT>and <TT>yourAge</TT> are declared to be variables of type <TT>unsigned short</TT>integer. In line 9, <TT>pAge</TT> is declared to be a pointer to an <TT>unsignedshort</TT> integer, and it is initialized with the address of the variable <TT>myAge</TT>.<BR>Lines 11 and 12 print the values and the addresses of <TT>myAge</TT> and <TT>yourAge</TT>.Line 14 prints the contents of <TT>pAge</TT>, which is the address of <TT>myAge</TT>.Line 15 prints the result of dereferencing <TT>pAge</TT>, which prints the valueat <TT>pAge</TT>--the value in <TT>myAge</TT>, or <TT>5</TT>.</P><P>This is the essence of pointers. Line 14 shows that <TT>pAge</TT> stores the addressof <TT>myAge</TT>, and line 15 shows how to get the value stored in <TT>myAge</TT>by dereferencing the pointer <TT>pAge</TT>. Make sure that you understand this fullybefore you go on. Study the code and look at the output.</P><P>In line 17, <TT>pAge</TT> is reassigned to point to the address of <TT>yourAge</TT>.The values and addresses are printed again. The output shows that <TT>pAge</TT> nowhas the address of the variable <TT>yourAge</TT> and that dereferencing obtains thevalue in <TT>yourAge</TT>.</P><P>Line 25 prints the address of <TT>pAge</TT> itself. Like any variable, it hasan address, and that address can be stored in a pointer. (Assigning the address ofa pointer to another pointer will be discussed shortly.)<BLOCKQUOTE> <P><HR><B>DO</B> use the indirection operator (<TT>*</TT>) to access the data stored at the address in a pointer. <B>DO</B> initialize all pointers either to a valid address or to <TT>null</TT> (<TT>0</TT>)<B>. DO </B>remember the difference between the address in a pointer and the value at that address. <HR></BLOCKQUOTE><H3 ALIGN="CENTER"><A NAME="Heading19"></A><FONT COLOR="#000077">Pointers</FONT></H3><P>To declare a pointer, write the type of the variable or object whose address willbe stored in the pointer, followed by the pointer operator (<TT>*</TT>) and the nameof the pointer. For example,</P><PRE><FONT COLOR="#0066FF">unsigned short int * pPointer = 0;</FONT></PRE><P>To assign or initialize a pointer, prepend the name of the variable whose addressis being assigned with the <TT>address of</TT> operator (<TT>&</TT>). For example,</P><PRE><FONT COLOR="#0066FF">unsigned short int theVariable = 5;unsigned short int * pPointer = & theVariable;</FONT></PRE><P>To dereference a pointer, prepend the pointer name with the dereference operator(<TT>*</TT>). For example,</P><PRE><FONT COLOR="#0066FF">unsigned short int theValue = *pPointer</FONT></PRE><H3 ALIGN="CENTER"><A NAME="Heading20"></A><FONT COLOR="#000077">Why Would You UsePointers?</FONT></H3><P>So far you've seen step-by-step details of assigning a variable's address to apointer. In practice, though, you would never do this. After all, why bother witha pointer when you already have a variable with access to that value? The only reasonfor this kind of pointer manipulation of an automatic variable is to demonstratehow pointers work. Now that you are comfortable with the syntax of pointers, youcan put them to good use. Pointers are used, most often, for three tasks:<UL> <LI>Managing data on the free store. <P> <LI>Accessing class member data and functions. <P> <LI>Passing variables by reference to functions.</UL><P>This rest of this chapter focuses on managing data on the free store and accessingclass member data and functions. Tomorrow you will learn about passing variablesby reference.<H3 ALIGN="CENTER"><A NAME="Heading21"></A><FONT COLOR="#000077">The Stack and theFree Store</FONT></H3><P>In the "Extra Credit" section following the discussion of functionsin Day 5, five areas of memory are mentioned:<UL> <LI>Global name space <P> <LI>The free store <P> <LI>Registers <P> <LI>Code space <P> <LI>The stack</UL><P>Local variables are on the stack, along with function parameters. Code is in codespace, of course, and global variables are in global name space. The registers areused for internal housekeeping functions, such as keeping track of the top of thestack and the instruction pointer. Just about all remaining memory is given overto the free store, which is sometimes referred to as the heap.</P><P>The problem with local variables is that they don't persist: When the functionreturns, the local variables are thrown away. Global variables solve that problemat the cost of unrestricted access throughout the program, which leads to the creationof code that is difficult to understand and maintain. Putting data in the free storesolves both of these problems.</P><P>You can think of the free store as a massive section of memory in which thousandsof sequentially numbered cubbyholes lie waiting for your data. You can't label thesecubbyholes, though, as you can with the stack. You must ask for the address of thecubbyhole that you reserve and then stash that address away in a pointer.</P><P>One way to think about this is with an analogy: A friend gives you the 800 numberfor Acme Mail Order. You go home and program your telephone with that number, andthen you throw away the piece of paper with the number on it. If you push the button,a telephone rings somewhere, and Acme Mail Order answers. You don't remember thenumber, and you don't know where the other telephone is located, but the button givesyou access to Acme Mail Order. Acme Mail Order is your data on the free store. Youdon't know where it is, but you know how to get to it. You access it by using itsaddress--in this case, the telephone number. You don't have to know that number;you just have to put it into a pointer (the button). The pointer gives you accessto your data without bothering you with the details.</P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -