Home arrow C++ arrow Page 3 - 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 - Pointer as a Variable
(Page 3 of 9 )

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

The Array Name as a Constant Pointer

While the pointer may be a variable, it also may be a constant. Indeed, in the previous chapter we actually discussed a constant pointer: the name of an array.

As you may recall from Chapter 10, the value of the name of an array is the base address of the array, which also is the address of the first element of an array. Thus, in the following program, both testScore and &testScore[0] have the same value.

#include <iostream>
using namespace std;
const int MAX = 3;
int main ()
{
   int testScore[MAX] = {4, 7, 1};
   cout << "The address of the array using testScore is "
       
<< testScore << endl;
   cout << "The address of the first element of the array "
      "using &testScore[0] is " << &testScore[0] << endl; 
   cout << "The value of the first element of the array "
      "using *testScore is " << *testScore << endl;
   cout << "The value of the first element of the array " 
      "using testScore[0] is " << testScore[0] << endl; 
   return 0;
}

The resulting output is

The address of the array using testScore is 0012FECC
The address of the first element of the array using &testScore[0] is 0012FECC
The value of the first element of the array using *testScore is 4
The value of the first element of the array using testScore[0] is 4

Similarly, if you dereference the name of an array, its value is the same as the value of the first element of the array. Therefore, in the preceding program, both *testScore and testScore[0] have the same value.

However, you cannot change the value of the name of the array. For example, a statement such as testScore++ would result in a compiler error, the error message being ď++ needs l-value.Ē As you may recall from Chapter 10, the term l-value refers to the value to the left of the assignment operator. This error message is another way of saying you canít increment a constant because that would be changing the value of a constant after you declare it.

Pointer Arithmetic

The value of a pointer, even though it is an address, is a numeric value. Therefore, you can perform arithmetic operations on a pointer just as you can a numeric value.

Using a Variable Pointer to Point to an Array

Pointer arithmetic is done often with arrays. However, since you cannot change the value of the name of an array, it being a constant pointer, you first should declare a variable pointer and then assign it to the address of an array.

So, we begin with an established point of reference, letís start with the following program, which outputs the address and value at each element of an array using the name of the array:

#include <iostream>
using namespace std;
const int MAX = 3;
int main ()
{
   int testScore[MAX] = {4, 7, 1};
   for (int i = 0; i < MAX; i++)
   {
     
cout << "The address of index " << i
         << " of the array is "<< &testScore[i] << endl;
      cout << "The value at index " << i
        
<< " of the array is "<< testScore[i] << endl;
   }
   return 0;
}

The resulting output is

The address of index 0 of the array is 0012FECC
The value at index 0 of the array is 4
The address of index 1 of the array is 0012FED0
The value at index 1 of the array is 7
The address of index 2 of the array is 0012FED4
The value at index 2 of the array is 1

This program used the name of the array, testScore, to access, by index, each element of the array. The name of the array is a constant pointer. The following program modifies the previous program by using a variable pointer, iPtr, to access by index each element of the array.

#include <iostream>
using namespace std;
const int MAX = 3;
int main ()
{
   int testScore[MAX] = {4, 7, 1};
   int* iPtr = testScore;
   for (int i = 0; i < MAX; i++)
   {
     cout << "The address of index " << i
       
<< " of the array is "<< & iPtr[i] << endl;
    
cout << "The value at index " << i
       
<< " of the array is "<< iPtr[i] << endl;
  
}
  
return 0;
}

The following statement in this program sets the variable pointer iPtr to point to the same address as the array name testScore:

int* iPtr = testScore;

The array name is not preceded with the address operator (&) because the array name already is an address, namely, the base address of the array. Therefore, after this assignment, iPtr and testScore both point to the beginning of the array. Accordingly, as shown in Figure 11-2, iPtr[2] and testScore[2] have the same value.


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