Home arrow C++ arrow Page 2 - What`s the Address? Pointers
C++

What`s the Address? Pointers


A pointer simply points to another variable or constant. Though they have a reputation for being difficult to learn, they really are not that hard. This is fortunate, becuase pointers are very important to programming. This article explains how to create and work with pointers in C++. It is excerpted from chapter 11 of the book C++ Demystified, written by Jeff Kent (McGraw-Hill, 2004; ISBN: 0072253703).

Author Info:
By: McGraw-Hill/Osborne
Rating: 5 stars5 stars5 stars5 stars5 stars / 27
August 11, 2005
TABLE OF CONTENTS:
  1. · What`s the Address? Pointers
  2. · Assigning a Value to a Pointer
  3. · Pointer as a Variable
  4. · Incrementing a Pointer
  5. · Passing an Array Using Pointer Notation
  6. · Passing a Single Variable Using Pointer Notation
  7. · Dynamic Memory Allocation
  8. · Returning Pointers from Functions
  9. · Summary

print this article
SEARCH DEVARTICLES

What`s the Address? Pointers - Assigning a Value to a Pointer
(Page 2 of 9 )

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 assignment, 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

Indirection Operator and Dereferencing

The primary use of a pointer is to access and, if appropriate, change the value of the variable that the pointer is pointing to. In the following program, the value of the integer variable num is changed twice.

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

The resulting output is

The value of num is 5
The value of num after num = 10 is 10
The value of num after *iPtr = 15 is 15

The first change should be familiar, by the direct assignment of a value to num, such as num=10. However, the second change is accomplished a new way, using the indirection operator:

*iPtr = 15;

The indirection operator is an asterisk, the same asterisk that you used to declare the pointer or to perform multiplication. However, in this statement the asterisk is not being used in a declaration or to perform multiplication, so in this context it is being used as an indirection operator.


NOTE:   As mentioned earlier in this chapter, this is another example of a symbol having different meanings in the C++ programming language depending on the context in which it was used.

The placement of the indirection operator before a pointer is said to dereference the pointer. Indeed, some texts refer to the indirection operator as the dereferencing operator. The value of a dereferenced pointer is not an address, but rather the value at that address—that is, the value of the variable that the pointer points to.

For example, in the preceding program, iPtr’s value is the address of num. However, the value of iPtr dereferenced is the value of num. Thus, the following two statements have the same effect, both changing the value of num:

num = 25;
*iPtr = 25;

Similarly, a dereferenced pointer can be used in arithmetic expressions the same as the variable to which it points. Thus, the following two statements have the same effect:

num *= 2;
*iPtr *= 2;

In these examples, changing a variable’s value using the indirection operator rather than through a straightforward assignment seems like an unnecessary complication. However, there are instances covered later in this chapter, such as looping through an array using a pointer, or using dynamic memory allocation, in which using the indirection operator is helpful or even necessary.

The Pointer as a Variable or a Constant

A pointer may be a variable or a constant. Let’s examine both possibilities.


blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials