Sams Teach Yourself C++ in 21 Days

(singke) #1

What Is a Pointer?................................................................................................


A pointer is a variable that holds a memory address. That’s it. If you understand this sim-
ple sentence, then you know the core of what there is to know about pointers.

A Bit About Memory......................................................................................


To understand pointers, you must know a little about computer memory. Computer mem-
ory is divided into sequentially numbered memory locations. Each variable is located at a
unique location in memory, known as its address. Figure 8.1 shows a schematic represen-
tation of the storage of an unsigned longinteger variable named theAge.

222 Day 8


The ability to use pointers and manipulate memory at a low level is one of
the factors that makes C++ the language of choice for embedded and real-
time applications.

NOTE

FIGURE8.1
A schematic
representation
of theAge.

theAge

100 101 102 103 104 105 106 107 108 109 110
each location = 1 byte
unsigned long int theAge = 4 bytes = 32 bits
variable name theAge points to 1st byte
the address of theAge is 102

1011
0101

0111
0110

1111
0110

1110
1110

Memory

Getting a Variable’s Memory Address............................................................


Different computers number this memory using different complex schemes. Usually, as a
programmer, you don’t need to know the particular address of any given variable because
the compiler handles the details. If you want this information, though, you can use the
address-of operator (&), which returns the address of an object in memory. Listing 8.1 is
used to illustrate the use of this operator.
Free download pdf