⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch11.htm

📁 一本好的VC学习书,本人就是使用这本书开始学习的vc,希望能对大家有帮助
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD><!-- This document was created from RTF source by rtftohtml version 3.0.1 -->	<META NAME="GENERATOR" Content="Symantec Visual Page 1.0">	<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">	<TITLE>Teach Yourself C++ in 21 Days</TITLE></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF"><H1 ALIGN="CENTER"><A HREF="ch10.htm" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/htm/ch10.htm"><IMG SRC="BLANPREV.GIF" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/buttons/BLANPREV.GIF"WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="tppmsgs/msgs0.htm#1" tppabs="http://www.mcp.com/sams"><IMGSRC="BLANHOME.GIF" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/buttons/BLANHOME.GIF" WIDTH="37" HEIGHT="37" ALIGN="BOTTOM"BORDER="0"></A><A HREF="index.htm" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/index.htm"><IMG SRC="BLANTOC.GIF" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/buttons/BLANTOC.GIF"WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="ch12.htm" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/htm/ch12.htm"><IMG SRC="BLANNEXT.GIF" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/buttons/BLANNEXT.GIF"WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A></H1><H1></H1><UL>	<LI><A HREF="#Heading1">Day 11</A>	<UL>		<LI><A HREF="#Heading2">Arrays</A>		<UL>			<LI><A HREF="#Heading3">What Is an Array?</A>			<UL>				<LI><A HREF="#Heading4">Figure 11.1.</A>			</UL>			<LI><A HREF="#Heading5">Array Elements</A>			<LI><A HREF="#Heading6">Listing 11.1. Using an integer array</A><A HREF="#Heading7">.</A>			<LI><A HREF="#Heading8">Writing Past the End of an Array</A>			<LI><A HREF="#Heading9">Listing 11.2. Writing past the end of an array</A><A HREF="#Heading10">.</A>			<LI><A HREF="#Heading11">Fence Post Errors</A>			<UL>				<LI><A HREF="#Heading12">Figure 11.2.</A>			</UL>			<LI><A HREF="#Heading13">Initializing Arrays</A>			<LI><A HREF="#Heading14">Declaring Arrays</A>			<LI><A HREF="#Heading15">Listing 11.3. Using consts and enums in arrays</A><A HREF="#Heading16">.</A>			<LI><A HREF="#Heading17">Arrays</A>			<LI><A HREF="#Heading18">Arrays of Objects</A>			<LI><A HREF="#Heading19">Listing 11.4. Creating an array of objects</A><A HREF="#Heading20">.</A>			<LI><A HREF="#Heading21">Multidimensional Arrays</A>			<UL>				<LI><A HREF="#Heading22">Figure 11.3.</A>			</UL>			<LI><A HREF="#Heading23">Initializing Multidimensional Arrays</A>			<LI><A HREF="#Heading24">Listing 11.5. Creating a multidimensional array</A><A HREF="#Heading25">.</A>			<UL>				<LI><A HREF="#Heading26">Figure 11.4.</A>			</UL>			<LI><A HREF="#Heading27">A Word About Memory</A>			<LI><A HREF="#Heading28">Arrays of Pointers</A>			<LI><A HREF="#Heading29">Listing 11.6. Storing an array on the free store</A><A HREF="#Heading30">.</A>			<LI><A HREF="#Heading31">Declaring Arrays on the Free Store</A>			<LI><A HREF="#Heading32">A Pointer to an Array Versus an Array of Pointers</A>			<LI><A HREF="#Heading33">Pointers and Array Names</A>			<LI><A HREF="#Heading34">Listing 11.7. Creating an array by using new</A><A HREF="#Heading35">.</A>			<LI><A HREF="#Heading36">Deleting Arrays on the Free Store</A>			<LI><A HREF="#Heading37">char Arrays</A>			<LI><A HREF="#Heading38">Listing 11.8. Filling an array</A><A HREF="#Heading39">.</A>			<LI><A HREF="#Heading40">Listing 11.9. Filling an array.</A>			<LI><A HREF="#Heading41">strcpy() and strncpy()</A>			<LI><A HREF="#Heading42">Listing 11.10. Using strcpy().</A>			<LI><A HREF="#Heading43">Listing 11.11. Using strncpy().</A>			<LI><A HREF="#Heading44">String Classes</A>			<LI><A HREF="#Heading45">Listing 11.12. Using a String class</A><A HREF="#Heading46">.</A>			<LI><A HREF="#Heading47">Linked Lists and Other Structures</A>			<UL>				<LI><A HREF="#Heading48">Figure 11.5.</A>			</UL>			<LI><A HREF="#Heading49">Listing 11.13. Implementing a linked list</A><A HREF="#Heading50">.</A>			<UL>				<LI><A HREF="#Heading51">Figure 11.6.</A>			</UL>			<LI><A HREF="#Heading52">Array Classes</A>			<LI><A HREF="#Heading53">Summary</A>			<LI><A HREF="#Heading54">Q&amp;A</A>			<LI><A HREF="#Heading55">Workshop</A>			<UL>				<LI><A HREF="#Heading56">Quiz</A>				<LI><A HREF="#Heading57">Exercises</A>			</UL>		</UL>	</UL></UL><P><HR SIZE="4"><H2 ALIGN="CENTER"><A NAME="Heading1"></A><FONT COLOR="#000077">Day 11</FONT></H2><H2 ALIGN="CENTER"><A NAME="Heading2"></A><FONT COLOR="#000077">Arrays</FONT></H2><P>In previous chapters, you declared a single <TT>int</TT>, <TT>char</TT>, or otherobject. You often want to declare a collection of objects, such as 20 <TT>int</TT>sor a litter of <TT>CAT</TT>s. Today, you learn<UL>	<LI>What arrays are and how to declare them.	<P>	<LI>What strings are and how to use character arrays to make them.	<P>	<LI>The relationship between arrays and pointers.	<P>	<LI>How to use pointer arithmetic with arrays.</UL><H3 ALIGN="CENTER"><A NAME="Heading3"></A><FONT COLOR="#000077">What Is an Array?</FONT></H3><P>An array is a collection of data storage locations, each of which holds the sametype of data. Each storage location is called an element of the array.</P><P>You declare an array by writing the type, followed by the array name and the subscript.The subscript is the number of elements in the array, surrounded by square brackets.For example,</P><PRE><FONT COLOR="#0066FF">long LongArray[25];</FONT></PRE><P>declares an array of 25 <TT>long</TT> integers, named <TT>LongArray</TT>. Whenthe compiler sees this declaration, it sets aside enough memory to hold all 25 elements.Because each <TT>long</TT> integer requires 4 bytes, this declaration sets aside100 contiguous bytes of memory, as illustrated in Figure 11.1.<BR><BR><A NAME="Heading4"></A><A HREF="11zcp01.jpg" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/art/ch11/11zcp01.jpg"><FONT COLOR="#000077">Figure11.1.</FONT></A><FONT COLOR="#000077"><I> </I></FONT><I>Declaring an array.</I><H3 ALIGN="CENTER"><A NAME="Heading5"></A><FONT COLOR="#000077">Array Elements</FONT></H3><P>You access each of the array elements by referring to an offset from the arrayname. Array elements are counted from zero. Therefore, the first array element is<TT>arrayName[0]</TT>. In the <TT>LongArray</TT> example, <TT>LongArray[0]</TT> isthe first array element, <TT>LongArray[1]</TT> the second, and so forth.</P><P>This can be somewhat confusing. The array <TT>SomeArray[3]</TT> has three elements.They are <TT>SomeArray[0]</TT>, <TT>SomeArray[1]</TT>, and <TT>SomeArray[2]</TT>.More generally, <TT>SomeArray[n]</TT> has n elements that are numbered <TT>SomeArray[0]</TT>through <TT>SomeArray[n-1]</TT>.</P><P>Therefore, <TT>LongArray[25]</TT> is numbered from <TT>LongArray[0]</TT> through<TT>LongArray[24]</TT>. Listing 11.1 shows how to declare an array of five integersand fill each with a value.</P><P><A NAME="Heading6"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 11.1. Using aninteger array.</B></FONT></P><PRE><FONT COLOR="#0066FF">1:     //Listing 11.1 - Arrays2:     #include &lt;iostream.h&gt;3:4:     int main()5:     {6:        int myArray[5];7:        int i;8:        for ( i=0; i&lt;5; i++)  // 0-49:        {10:           cout &lt;&lt; &quot;Value for myArray[&quot; &lt;&lt; i &lt;&lt; &quot;]: &quot;;11:          cin &gt;&gt; myArray[i];12:       }13:       for (i = 0; i&lt;5; i++)14:          cout &lt;&lt; i &lt;&lt; &quot;: &quot; &lt;&lt; myArray[i] &lt;&lt; &quot;\n&quot;;15:     return 0;<TT>16: }</TT>Output: Value for myArray[0]:  3Value for myArray[1]:  6Value for myArray[2]:  9Value for myArray[3]:  12Value for myArray[4]:  150: 31: 62: 93: 124: 15</FONT></PRE><P><FONT COLOR="#000077"><B>Analysis:</B></FONT><B> </B>Line 6 declares an arraycalled <TT>myArray</TT>, which holds five integer variables. Line 8 establishes aloop that counts from 0 through 4, which is the proper set of offsets for a five-elementarray. The user is prompted for a value, and that value is saved at the correct offsetinto the array.<BR><BR>The first value is saved at <TT>myArray[0]</TT>, the second at <TT>myArray[1]</TT>,and so forth. The second <TT>for</TT> loop prints each value to the screen.<BLOCKQUOTE>	<P><HR><FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>Arrays count from 0, not from 1.	This is the cause of many bugs in programs written by C++ novices. Whenever you use	an array, remember that an array with 10 elements counts from <TT>ArrayName[0]</TT>	to <TT>ArrayName[9]</TT>. There is no <TT>ArrayName[10]</TT>. <HR></BLOCKQUOTE><H3 ALIGN="CENTER"><A NAME="Heading8"></A><FONT COLOR="#000077">Writing Past theEnd of an Array</FONT></H3><P>When you write a value to an element in an array, the compiler computes whereto store the value based on the size of each element and the subscript. Suppose thatyou ask to write over the value at <TT>LongArray[5]</TT>, which is the sixth element.The compiler multiplies the offset (5) by the size of each element--in this case,4. It then moves that many bytes (20) from the beginning of the array and writesthe new value at that location.</P><P>If you ask to write at <TT>LongArray[50]</TT>, the compiler ignores the fact thatthere is no such element. It computes how far past the first element it should look(200 bytes) and then writes over whatever is at that location. This can be virtuallyany data, and writing your new value there might have unpredictable results. If you'relucky, your program will crash immediately. If you're unlucky, you'll get strangeresults much later in your program, and you'll have a difficult time figuring outwhat went wrong.</P><P>The compiler is like a blind man pacing off the distance from a house. He startsout at the first house, <TT>MainStreet[0]</TT>. When you ask him to go to the sixthhouse on Main Street, he says to himself, &quot;I must go five more houses. Eachhouse is four big paces. I must go an additional 20 steps.&quot; If you ask him togo to <TT>MainStreet[100]</TT>, and Main Street is only 25 houses long, he will paceoff 400 steps. Long before he gets there, he will, no doubt, step in front of a movingbus. So be careful where you send him.</P><P>Listing 11.2 shows what happens when you write past the end of an array.<BLOCKQUOTE>	<P><HR><FONT COLOR="#000077"><B>WARNING:</B></FONT><B> </B>Do not run this program; it may	crash your system! <HR></BLOCKQUOTE><P><A NAME="Heading9"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 11.2. Writingpast the end of an array.</B></FONT></P><PRE><FONT COLOR="#0066FF">1:     //Listing 11.22:     // Demonstrates what happens when you write past the end3:     // of an array4:5:     #include &lt;iostream.h&gt;6:     int main()7:     {8:        // sentinels9:        long sentinelOne[3];10:       long TargetArray[25]; // array to fill11:       long sentinelTwo[3];12:       int i;13:       for (i=0; i&lt;3; i++)14:          sentinelOne[i] = sentinelTwo[i] = 0;15:16:       for (i=0; i&lt;25; i++)17:          TargetArray[i] = 0;18:19:       cout &lt;&lt; &quot;Test 1: \n&quot;;  // test current values (should be 0)20:       cout &lt;&lt; &quot;TargetArray[0]: &quot; &lt;&lt; TargetArray[0] &lt;&lt; &quot;\n&quot;;21:       cout &lt;&lt; &quot;TargetArray[24]: &quot; &lt;&lt; TargetArray[24] &lt;&lt; &quot;\n\n&quot;;22:23:       for (i = 0; i&lt;3; i++)24:       {25:          cout &lt;&lt; &quot;sentinelOne[&quot; &lt;&lt; i &lt;&lt; &quot;]: &quot;;26:          cout &lt;&lt; sentinelOne[i] &lt;&lt; &quot;\n&quot;;27:          cout &lt;&lt; &quot;sentinelTwo[&quot; &lt;&lt; i &lt;&lt; &quot;]: &quot;;28:           cout &lt;&lt; sentinelTwo[i]&lt;&lt; &quot;\n&quot;;29:       }30:31:       cout &lt;&lt; &quot;\nAssigning...&quot;;32:       for (i = 0; i&lt;=25; i++)33:          TargetArray[i] = 20;34:35:       cout &lt;&lt; &quot;\nTest 2: \n&quot;;36:       cout &lt;&lt; &quot;TargetArray[0]: &quot; &lt;&lt; TargetArray[0] &lt;&lt; &quot;\n&quot;;37:       cout &lt;&lt; &quot;TargetArray[24]: &quot; &lt;&lt; TargetArray[24] &lt;&lt; &quot;\n&quot;;38:       cout &lt;&lt; &quot;TargetArray[25]: &quot; &lt;&lt; TargetArray[25] &lt;&lt; &quot;\n\n&quot;;39:       for (i = 0; i&lt;3; i++)40:       {41:          cout &lt;&lt; &quot;sentinelOne[&quot; &lt;&lt; i &lt;&lt; &quot;]: &quot;;42:          cout &lt;&lt; sentinelOne[i]&lt;&lt; &quot;\n&quot;;43:          cout &lt;&lt; &quot;sentinelTwo[&quot; &lt;&lt; i &lt;&lt; &quot;]: &quot;;44:          cout &lt;&lt; sentinelTwo[i]&lt;&lt; &quot;\n&quot;;45:       }46:47:     return 0;<TT>48: }</TT></FONT>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -