Assigning a Value to a Pointer


This section will explain how you assign a value to a pointer. Though, before I explain how, perhaps I should explain why.

Why You Should Not Try to Use an Unassigned  Pointer

Back in elementary school we were taught a verse: I shot an arrow into the air, where it lands, I don t care. Looking back, I wonder why young children were taught this verse. It may rhyme , but its message is really not appropriate for little ones. However, when you declare a pointer but then use it without first assigning it a value, you are, alas, doing the programming equivalent of that verse.

The following program declares a pointer and then attempts to output its value without first assigning it a value:

 #include <iostream> using namespace std; int main () {  int* iPtr;  cout << "The value of iPtr is " << iPtr << endl;  return 0; } 

The result, depending on your compiler and operating system, may be a compiler error, a runtime error, or a computer that locks up. Regardless, attempting to use a declared pointer without first assigning it a value is not a good idea.

As you may recall from previous chapters, when you declare a variable and then attempt to output its value without first assigning it a value, the result is a so-called garbage value that makes little sense. The reason for this result is that the computer attempts to interpret whatever value is left over from previous programs at the address of the variable.

When the variable is a pointer, that leftover value is interpreted as another memory address, which the pointer then tries to access when you attempt to use it. There are a number of memory address ranges that you are not permitted to access programmatically, such as those reserved for use by the operating system. If the leftover value is interpreted as one of those prohibited addresses, the result is an error.

Null Pointers

If it is too early in your code to know which address to assign to the pointer, then you first assign the pointer NULL, which is a constant with a value of zero defined in several standard libraries, including iostream. The following program does so:

 #include <iostream> using namespace std; int main () {  int* iPtr;  iPtr = NULL;  cout << "The value of iPtr is " << iPtr << endl;  return 0; } 
Note  

You also could use initialization instead of declaration followed by assign-ment, thus combining the first two statements in main to int* iPtr = NULL.

The resulting output is

 The address of x using iPtr is 00000000 

A pointer that is assigned NULL is called a null pointer.

On most operating systems, programs are not permitted to access memory at address 0 because that memory is reserved by the operating system. You may now be thinking: Wait a minute! He just told me how bad it was to risk having pointers point to memory addresses reserved by the operating system. Now he s having us do that on purpose. However, the memory address 0 has special significance; it signals that the pointer is not intended to point to an accessible memory location. Thus, if it is too early in your code to know which address to assign to a pointer, you should first assign the pointer to NULL, which then makes it safe to access the value of a pointer before it is assigned a real value such as the address of another variable or constant.

Assigning a Pointer the Address of a  Variable  or  Constant

Let s now assign a pointer a real value, the address of another variable or constant. To do so, you need to access the address of the variable or constant before you can assign that address to the pointer. You use the address operator, covered in Chapter 3, to accomplish this task.

The following program shows how to use the address operator to assign the address of a variable to a pointer. This program also demonstrates that the value of a pointer is the same as the address to which the pointer points.

 #include <iostream> using namespace std; int main () {  int num = 5;  int* iPtr = &num;  cout << "The address of x using &num is " << &num << endl;  cout << "The address of x using iPtr is " << iPtr << endl;  return 0; } 

The output on my computer (the following addresses likely will be different on yours) is

 The address of x using &num is 0012FED4 The address of x using iPtr is 0012FED4 

Figure 11-1 shows graphically how the pointer points to the integer variable.


Figure 11-1: Pointer pointing to an integer variable



C++ Demystified(c) A Self-Teaching Guide
C++ Demystified(c) A Self-Teaching Guide
ISBN: 72253703
EAN: N/A
Year: 2006
Pages: 148

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