Indirect Data Access The variables that we have dealt with so far provide you with the ability to name a memory location in which you can store data of a particular type. The contents of a variable are either entered from an external source, such as the keyboard, or calculated from other values that are entered. There is another kind of variable in C++ which does not store data that you normally enter or calculate, but greatly extends the power and flexibility of your programs. This kind of variable is called a pointer. What is a Pointer? Each memory location that you use to store a data value has an address. The address provides the means for your PC hardware to reference a particular data item. A pointer is a variable that stores an address of another variable of a particular type. A pointer has a variable name just like any other variable and also has a type which designates what kind of variables its contents refer to. Note that the type of a pointer variable includes the fact that it's a pointer. A variable that is a pointer which can contain addresses of locations in memory containing values of type int, is of type 'pointer to int'. Declaring Pointers The declaration for a pointer is similar to that of an ordinary variable, except that the pointer name has an asterisk in front of it to indicate that it's a variable which is a pointer. For example, to declare a pointer pnumber that points to a variable of type long, you could use the following statement: long* pnumber; This declaration has been written with the asterisk close to the type name. If you wish, you can also write it as: long *pnumber; The compiler won't mind at all. However, remember that the type of the variable pnumber is 'pointer to long', which is often indicated by placing the asterisk close to the type name. You can mix declarations of ordinary variables and pointers in the same statement. For example: long* pnumber, number = 99; This declares the pointer pnumber of type 'pointer to long' as before, and also declares the variable number, of type long. On balance, it's probably better to declare pointers separately from other variables, otherwise the statement can appear misleading as to the type of the variables declared, particularly if you prefer to place the * adjacent to the type name. The following statements certainly look clearer and putting declarations on separate lines enables you to add comments for them individually, making for a program that is easier to read: // Declaration and initialization of long variable long number = 99; // Declaration of variable of type pointer to long long* pnumber; It's a common convention in C++ to use variable names beginning with p to denote pointers. This makes it easier to see which variables in a program are pointers, which in turn can make a program easier to follow. Let's take an example to see how this works, without worrying about what it's for. We will come on to how this is used very shortly. Suppose we have the long integer variable number, as we declared it above containing the value 99. We also have the pointer, pnumber, of type pointer to long, which we could use to store the address of our variable number. But how can we obtain the address of a variable? The Address-Of Operator What we need is the address-of operator, &. This is a unary operator which obtains the address of a variable. It's also called the reference operator, for reasons we will discuss later in this chapter. To set up the pointer that we have just discussed, we could write this assignment statement: pnumber = &number; // Store address of number in pnumber The result of this operation is illustrated below: You can use the operator & to obtain the address of any variable, but you need a pointer of the same type to store it. If you want to store the address of a double variable for example, the pointer must have been declared as double*, which is type 'pointer to double'. Using Pointers Taking the address of a variable and storing it in a pointer is all very well, but the really interesting aspect is how you can use it. Fundamental to using a pointer is accessing the data value in the variable to which a pointer points. This is done using the indirection operator, *. The Indirection Operator The indirection operator, *, is used with a pointer to access the contents of the variable to which it points. The name 'indirection operator' stems from the fact that the data is accessed indirectly. It is also called the de-reference operator, and the process of accessing the data in the variable pointed to by a pointer is termed de-referencing the pointer. One aspect of this operator that can seem confusing is the fact that we now have several different uses for the same symbol, *. It is the multiply operator, the indirection operator, and it is used in the declaration of a pointer. Each time you use *, the compiler is able to distinguish its meaning by the context. When you multiply two variables, A*B for instance, then there's no meaningful interpretation of this expression for anything other than a multiply operation. Why Use Pointers? A question that usually springs to mind at this point is, 'Why use pointers at all?' After all, taking the address of a variable you already know and sticking it in a pointer so that you can de-reference it seems like an overhead you can do without. There a several reasons why pointers are important.