Declaring and Using Pointers


You declare a pointer variable by stating the type of the variable, the fact that it's a pointer, and the name of the variable. Here's an example:

 int *anIntPointer; int anInt = 100; 


The variable anIntPointer points at integers, as the type name int indicates. The asterisk indicates that the variable is a pointer to integers rather than a variable that contains integers. The second variable declaration does not have the asterisk, so anInt is a variable that contains an integer.

You store an address in a variable with the ampersand operator, like this:

 int *anIntPointer; int anInt = 100; anInputPointer = &anInt; 


The third of these three statements stores the address of anInt into the pointer variable anInputPointer. As a result, the variable anIntPointer points to the location in memory where anInt resides. Figure 11.1 represents this graphically.

Figure 11.1. Storing an address in a pointer variable.


In Figure 11.1, the pointer is represented by an arrow. The arrow is a graphical way of depicting the fact that the pointer variable anIntPointer stores the address of anInt. Your program can access the data in anInt through the pointer. To do so, you use the asterisk operator. Listing 11.1 gives a short example of how this is done.

Listing 11.1. A program with a pointer

 1     #include <cstdlib> 2     #include <iostream> 3 4     using namespace std; 5 6     int main(int argc, char *argv[]) 7     { 8          int anInt = 5; 9          int *anIntPointer = &anInt; 10 11         cout << "anInt = " << anInt << endl; 12         cout << "*anIntPointer = " << *anIntPointer << endl; 13         cout << endl; 14 15         *anIntPointer = 100; 16 17         cout << "anInt = " << anInt << endl; 18         cout << "*anIntPointer = " << *anIntPointer << endl; 19         cout << endl; 20 21         anInt = 50; 22 23         cout << "anInt = " << anInt << endl; 24         cout << "*anIntPointer = " << *anIntPointer << endl; 25         cout << endl; 26 27         system("PAUSE"); 28         return EXIT_SUCCESS; 29    } 

Line 8 of Listing 11.1 declares an integer variable and stores a 5 in it. Line 9 creates an integer pointer (a pointer to an integer) and sets it to point to the variable anInt. Line 11 prints the value in anInt, which is 5. On line 12, the program prints the value in the location that anIntPointer points to. Because anIntPointer points to anInt, the statement on line 12 prints the 5 again.

Knowing the Pointer Lingo

Programmers have specific ways of talking about pointers that help everyone understand what they're talking about. For example, programmers often read a statement like *anIntPointer as "the contents of anIntPointer." However, that's not accurate. A more accurate way to read it is "the contents of the location that *anIntPointer points to." However, most programmers find this too wordy, so they use the shorter and less accurate reading.

There's a more technical term for statements that access the contents of the location a pointer points at. Programmers call it a dereference. Anytime programmers say anything about dereferencing a pointer, they're talking about accessing the memory location where the pointer points.


Line 15 of Listing 11.1 stores the value 100 in the location that anIntPointer points to. The statements on lines 1718 prove that doing so changes the value in anInt because anInt and anIntPointer both refer to the same memory location. This is proven again on lines 2125. The assignment statement on line 21 stores a 50 in anInt. The statements in lines 2324 show that this changes both anInt and *anIntPointer because both anInt and *anIntPointer refer to the same memory location.

Is this confusing? If it is, you're having a very normal experience. Pointers are hard to learn. We all struggle with them when we first encounter them. You'll become proficient at them through practice, practice, and more practice. Anyone can master pointers and everyone feels frustrated with them at first.

If you're wondering whether learning pointers is worthwhile, the answer is a big YES. I'll show you why shortly. However, before diving into that, I just want to quickly review what I've presented so far.

Declare a pointer variable by adding an asterisk to the declaration just before the variable name, like this:

 int *anIntPointer; 

Take the address of a variable with the ampersand, like this:

 &anInt 

Store addresses in pointer variables, like this:

 int anInt; int *anIntPointer; anInputPointer = &anInt; 

Access the contents of the memory location a pointer points at with the asterisk, like this:

 *anIntPointer 

These four points are what you need to know to declare and use pointers.



Creating Games in C++(c) A Step-by-Step Guide
Creating Games in C++: A Step-by-Step Guide
ISBN: 0735714347
EAN: 2147483647
Year: N/A
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net