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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

68 Day 3<br />

Po<strong>in</strong>ters: Welcome to My Nightmare<br />

Po<strong>in</strong>ters are one of the most confus<strong>in</strong>g aspects of the <strong>C++</strong> language. They are also one of the<br />

most powerful features of <strong>C++</strong>. My goal <strong>in</strong> this section is not to teach you the textbook<br />

def<strong>in</strong>ition of po<strong>in</strong>ters, but rather to teach you po<strong>in</strong>ters <strong>in</strong> the context of how you will use them<br />

<strong>in</strong> your <strong>C++</strong>Builder programs. So what is a po<strong>in</strong>ter? It’s a variable that holds the address of<br />

another variable. There, that wasn’t so bad, was it? I wish it were that simple! Because a po<strong>in</strong>ter<br />

holds the address of another variable, it is said to “po<strong>in</strong>t to” the second variable. This is called<br />

<strong>in</strong>direction because the po<strong>in</strong>ter does not have a direct association with the actual data, but<br />

rather an <strong>in</strong>direct association.<br />

A po<strong>in</strong>ter is a variable that holds the address of another variable.<br />

NEW TERM<br />

NEW TERM<br />

NOTE<br />

Because the po<strong>in</strong>ter does not have a direct association with the actual data,<br />

<strong>in</strong>direction is the term used when referr<strong>in</strong>g to this <strong>in</strong>direct association.<br />

Let’s look at an example. Earlier we talked about arrays. Let’s say that you had an array of <strong>in</strong>ts.<br />

You could access the <strong>in</strong>dividual elements of the array us<strong>in</strong>g the subscript operator, as I talked<br />

about on Day 1, “Gett<strong>in</strong>g Your Feet Wet”:<br />

<strong>in</strong>t array[] = { 5, 10, 15, 20, 25 };<br />

<strong>in</strong>t someVariable = array[3]; // the value 20<br />

You could also use a po<strong>in</strong>ter to accomplish the same th<strong>in</strong>g:<br />

<strong>in</strong>t array[] = { 5, 10, 15, 20, 25 };<br />

<strong>in</strong>t* ptr = array;<br />

<strong>in</strong>t someVariable = ptr[3];<br />

In this example, the memory location of the beg<strong>in</strong>n<strong>in</strong>g of the array is assigned to the po<strong>in</strong>ter<br />

named ptr. Note that the po<strong>in</strong>ter is a po<strong>in</strong>ter of the data type <strong>in</strong>t and that the <strong>in</strong>direction<br />

operator (the * symbol) is used when you declare a po<strong>in</strong>ter. You can declare a po<strong>in</strong>ter to any<br />

of the <strong>in</strong>tegral data types (<strong>in</strong>t, char, long, short, and so on), as well as a po<strong>in</strong>ter to objects<br />

(structures or classes). After the assignment the po<strong>in</strong>ter conta<strong>in</strong>s the memory address of the<br />

start of the array, and as such po<strong>in</strong>ts to the array.<br />

The name of an array variable, when used without the subscript<br />

operator, returns the memory address of the first element of the array.<br />

Put another way, the variable name of an array is a po<strong>in</strong>ter, to the start<br />

of the array. That makes it possible to assign an array to a po<strong>in</strong>ter, as <strong>in</strong><br />

the preced<strong>in</strong>g example.

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

Saved successfully!

Ooh no, something went wrong!