12.12.2012 Views

Teach Yourself Borland C++ in 14 Days - portal

Teach Yourself Borland C++ in 14 Days - portal

Teach Yourself Borland C++ in 14 Days - portal

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Gett<strong>in</strong>g Your Feet Wet<br />

There is a shortcut method to declar<strong>in</strong>g and fill<strong>in</strong>g an array all at one time. It looks like this:<br />

<strong>in</strong>t myArray[5] = { -200, -100, 0, 100, 200 };<br />

To take this one step further, if you know exactly how many elements your array will have,<br />

and if you fill the array when you declare it, you can even leave out the array size when you<br />

declare the array. In that case you would use the follow<strong>in</strong>g:<br />

<strong>in</strong>t myArray[] = { -200, -100, 0, 100, 200 };<br />

This works because the compiler can figure out from the list of values be<strong>in</strong>g assigned how<br />

many elements are <strong>in</strong> the array and how much memory to allocate for the array.<br />

Arrays can be multidimensional. To create a two-dimensional array of <strong>in</strong>tegers, you would<br />

use code like this:<br />

<strong>in</strong>t mdArray[3][5];<br />

This allocates storage for 15 <strong>in</strong>ts (a total of 60 bytes, if you’re keep<strong>in</strong>g score). You access<br />

elements of the array like you do a simple array, with the obvious difference that you must<br />

supply two subscript operators:<br />

<strong>in</strong>t x = mdArray[1][1] + mdArray[2][1];<br />

Figure 1.8 illustrates how a two-dimensional array might look <strong>in</strong> memory.<br />

Figure 1.8.<br />

A two-dimensional<br />

array <strong>in</strong> memory.<br />

WARNING<br />

Array[0][]<br />

Array[1][]<br />

Array[2][]<br />

myArray[][0] myArray[][1] myArray[][2] myArray[][3] myArray[][4]<br />

baseAddr baseAddr+4 baseAddr+8 baseAddr+12 baseAddr+16<br />

baseAddr+20 baseAddr+24 baseAddr+28 baseAddr+32 baseAddr+36<br />

baseAddr+40 baseAddr+44 baseAddr+48 baseAddr+52 baseAddr+56<br />

You must be careful not to overwrite the end of an array. One powerful<br />

feature of <strong>C++</strong> is direct access to memory. Because of this feature, <strong>C++</strong><br />

will not prevent you from writ<strong>in</strong>g to a particular memory location even<br />

if that location is memory your program isn’t supposed to have access<br />

to. The follow<strong>in</strong>g code is legal, but will result <strong>in</strong> a crash <strong>in</strong> your<br />

program (or <strong>in</strong> W<strong>in</strong>dows):<br />

<strong>in</strong>t array[5];<br />

array[5] = 10;<br />

This is a common error to make because you might th<strong>in</strong>k the last<br />

element of this array is 5 when it is really 4. If you overwrite the end of<br />

an array, you have no idea what memory you are overwrit<strong>in</strong>g. The<br />

29<br />

1

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!