William Shakespeare,HamletApointeris the memory address of a variable. Recall from Chapter 5 that thecomputer’s memory is divided into numbered memory locations (calledbytes)and that variables are implemented as a sequence of adjacent memory loca-tions. Recall also that sometimes the C++ system uses these memory addressesas names for the variables. If a variable is implemented as, say, three memorylocations, then the address of the first of these memory locations is sometimes10.1
Pointers405used as a name for that variable. For example, when the variable is used as a call-by-reference argument, it is this address, not the identifier name of the variable, that ispassed to the calling function. An address that is used to name a variable in this way (bygiving the address in memory where the variable starts) is called apointerbecause theaddress can be thought of as “pointing” to the variable. The address “points” to the vari-able because it identifies the variable by tellingwherethe variable is, rather than tellingwhat the variable’s name is.You have already been using pointers in a number of situations. As noted in the pre-vious paragraph, when a variable is a call-by-reference argument in a function call, thefunction is given this argument variable in the form of a pointer to the variable. Asnoted in Chapter 5, an array is given to a function (or to anything else, for that matter)by giving a pointer to the first array element. (At the time we called these pointers“memory addresses,” but that is the same thing as a pointer.) These are two powerfuluses for pointers, but they are handled automatically by the C++ system. This chaptershows you how to write programs that directly manipulate pointers rather than relyingon the system to manipulate the pointers for you.■POINTER VARIABLESA pointer can be stored in a variable. However, even though a pointer is a memoryaddress and a memory address is a number, you cannot store a pointer in a variable oftypeintordouble. A variable to hold a pointer must be declared to have a pointertype. For example, the following declarespto be a pointer variable that can hold onepointer that points to a variable of typedouble:double*p;The variablepcan hold pointers to variables of typedouble, but it cannot normallycontain a pointer to a variable of some other type, such asintorchar. Each variabletype requires a different pointer type.1In general, to declare a variable that can hold pointers to other variables of a specifictype, you declare the pointer variable just as you would declare an ordinary variable ofthat type, but you place an asterisk in front of the variable name. For example, the fol-lowing declares the variablesp1andp2so they can hold pointers to variables of typeint; it also declares two ordinary variablesv1andv2of typeint:int*p1, *p2, v1, v2;There must be an asterisk beforeeachof the pointer variables. If you omit the secondasterisk in the above declaration, thenp2will not be a pointer variable; it will instead bean ordinary variable of typeint.
Upload your study docs or become a
Course Hero member to access this document
Upload your study docs or become a
Course Hero member to access this document
End of preview. Want to read all 847 pages?
Upload your study docs or become a
Course Hero member to access this document
Term
Fall
Professor
NoProfessor
Tags