?? ch11.htm
字號:
<!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://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/htm/ch10.htm"><IMG SRC="../buttons/BLANPREV.GIF" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/buttons/BLANPREV.GIF"
WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="javascript:if(confirm('http://www.mcp.com/sams \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://www.mcp.com/sams'" tppabs="http://www.mcp.com/sams"><IMG
SRC="../buttons/BLANHOME.GIF" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/buttons/BLANHOME.GIF" WIDTH="37" HEIGHT="37" ALIGN="BOTTOM"
BORDER="0"></A><A HREF="../index.htm" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/index.htm"><IMG SRC="../buttons/BLANTOC.GIF" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/buttons/BLANTOC.GIF"
WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="ch12.htm" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/htm/ch12.htm"><IMG SRC="../buttons/BLANNEXT.GIF" tppabs="http://petunia.atomki.hu/pio/Manuals/english/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&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 other
object. You often want to declare a collection of objects, such as 20 <TT>int</TT>s
or 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 same
type 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>. When
the 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 aside
100 contiguous bytes of memory, as illustrated in Figure 11.1.<BR>
<BR>
<A NAME="Heading4"></A><A HREF="javascript:if(confirm('http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/art/ch11/11zcp01.jpg \n\nThis file was not retrieved by Teleport Pro, because the server reports that this file cannot be found. \n\nDo you want to open it from the server?'))window.location='http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/art/ch11/11zcp01.jpg'" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/art/ch11/11zcp01.jpg"><FONT COLOR="#000077">Figure
11.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 array
name. 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> is
the 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 integers
and fill each with a value.</P>
<P><A NAME="Heading6"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 11.1. Using an
integer array.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">
1: //Listing 11.1 - Arrays
2: #include <iostream.h>
3:
4: int main()
5: {
6: int myArray[5];
7: int i;
8: for ( i=0; i<5; i++) // 0-4
9: {
10: cout << "Value for myArray[" << i << "]: ";
11: cin >> myArray[i];
12: }
13: for (i = 0; i<5; i++)
14: cout << i << ": " << myArray[i] << "\n";
15: return 0;
<TT>16: }</TT>
Output: Value for myArray[0]: 3
Value for myArray[1]: 6
Value for myArray[2]: 9
Value for myArray[3]: 12
Value for myArray[4]: 15
0: 3
1: 6
2: 9
3: 12
4: 15
</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis:</B></FONT><B> </B>Line 6 declares an array
called <TT>myArray</TT>, which holds five integer variables. Line 8 establishes a
loop that counts from 0 through 4, which is the proper set of offsets for a five-element
array. The user is prompted for a value, and that value is saved at the correct offset
into 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 the
End of an Array</FONT></H3>
<P>When you write a value to an element in an array, the compiler computes where
to store the value based on the size of each element and the subscript. Suppose that
you 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 writes
the new value at that location.</P>
<P>If you ask to write at <TT>LongArray[50]</TT>, the compiler ignores the fact that
there 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 virtually
any data, and writing your new value there might have unpredictable results. If you're
lucky, your program will crash immediately. If you're unlucky, you'll get strange
results much later in your program, and you'll have a difficult time figuring out
what went wrong.</P>
<P>The compiler is like a blind man pacing off the distance from a house. He starts
out at the first house, <TT>MainStreet[0]</TT>. When you ask him to go to the sixth
house on Main Street, he says to himself, "I must go five more houses. Each
house is four big paces. I must go an additional 20 steps." If you ask him to
go to <TT>MainStreet[100]</TT>, and Main Street is only 25 houses long, he will pace
off 400 steps. Long before he gets there, he will, no doubt, step in front of a moving
bus. 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. Writing
past the end of an array.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">
1: //Listing 11.2
2: // Demonstrates what happens when you write past the end
3: // of an array
4:
5: #include <iostream.h>
6: int main()
7: {
8: // sentinels
9: long sentinelOne[3];
10: long TargetArray[25]; // array to fill
11: long sentinelTwo[3];
12: int i;
13: for (i=0; i<3; i++)
14: sentinelOne[i] = sentinelTwo[i] = 0;
15:
16: for (i=0; i<25; i++)
17: TargetArray[i] = 0;
18:
19: cout << "Test 1: \n"; // test current values (should be 0)
20: cout << "TargetArray[0]: " << TargetArray[0] << "\n";
21: cout << "TargetArray[24]: " << TargetArray[24] << "\n\n";
22:
23: for (i = 0; i<3; i++)
24: {
25: cout << "sentinelOne[" << i << "]: ";
26: cout << sentinelOne[i] << "\n";
27: cout << "sentinelTwo[" << i << "]: ";
28: cout << sentinelTwo[i]<< "\n";
29: }
30:
31: cout << "\nAssigning...";
32: for (i = 0; i<=25; i++)
33: TargetArray[i] = 20;
34:
35: cout << "\nTest 2: \n";
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -