< Previous | Contents | Next >

Understanding Pointer Basics

Pointers have a reputation for being difficult to understand. In reality, the essence of pointers is quite simplea pointer is a variable that can contain a memory address. Pointers give you the ability to work directly and efficiently with computer memory. Like iterators from the STL, theyre often used to access the contents of other variables. But before you can put pointers to good use in your game programs, you have to understand the basics of how they work.


223

224 Chapter 7 n Pointers: Tic-Tac-Toe 2.0


Hi n t

image

Computer memory is a lot like a neighborhood, but instead of houses in which people store their stuff, you have memory locations where you can store data. Just like a neighborhood where houses sit side by side, labeled with addresses, chunks of computer memory sit side by side, labeled with addresses. In a neighborhood, you can use a slip of paper with a street address on it to get to a particular house (and to the stuff stored inside it). In a computer, you can use a pointer with a memory address in it to get to a particular memory location (and to the stuff stored inside it).

image


 

Introducing the Pointing ProgramDeclaring PointersInitializing PointersAssigning Addresses to PointersDereferencing PointersReassigning PointersUsing Pointers to Objects