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

Assigning a Pointer the Address of a Variable or Constant - Practices

The name "pointer" describes the job of the item; a pointer "points" to another variable or constant. Some tasks in C++ are easier to do with pointers; others would be utterly impossible without pointers. This article discusses how to create and work with pointers in C++. It is taken from chapter 11 of the book C++ Demystified, written by Jeff Kent (McGraw-Hill/Osborne, 2004; ISBN: 0072253703).

TABLE OF CONTENTS:
  1. What`s the Address? Pointers
  2. Assigning a Pointer the Address of a Variable or Constant
  3. The Array Name as a Constant Pointer
  4. Incrementing a Pointer
  5. Passing an Array Using Pointer Notation
  6. Dynamic Memory Allocation
  7. Returning Pointers from Function s
  8. Summary
By: McGraw-Hill/Osborne
Rating: starstarstarstarstar / 16
September 01, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.

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.

Pointer as a Variable

The preceding program had the pointer pointing to one integer variable. However, a pointer variable, being a variable, can point to different variables at different times in the program. In the following program, the value of the pointer is changed to point to two different integer variables. 

#include <iostream>> using namespace std;

int main ()
{
  int num1 = 5, num2 = 14;
  int* iPtr = &num1;
  cout << "The value of num1 is " << num1 << endl;
  *iPtr *= 2;
  cout << "The value of num1 after *iPtr *= 2 is "
<< *iPtr << endl;
  iPtr = &num2;
  cout << "The value of num2 is " << num2 << endl;
  *iPtr /= 2;
   cout << "The value of num after *iPtr /= 2 is "
<< *iPtr << endl;
  return 0;
}

The resulting output is therefore:

The value of num1 is 5
The value of num1 after *iPtr *= 2 is 10
The value of num2 is 14
The value of num after *iPtr /= 2 is 7



 
 
>>> More Practices Articles          >>> More By McGraw-Hill/Osborne
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PRACTICES ARTICLES

- Calculating Development Project Costs
- More Techniques for Finding Things
- Finding Things
- Finishing the System`s Outlines
- The System in So Many Words
- Basic Data Types and Calculations
- What`s the Address? Pointers
- Design with ArgoUML
- Pragmatic Guidelines: Diagrams That Work
- Five-Step UML: OOAD for Short Attention Span...
- Five-Step UML: OOAD for Short Attention Span...
- Introducing UML: Object-Oriented Analysis an...
- Class and Object Diagrams
- Class Relationships
- Classes

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: