Practices
  Home arrow Practices arrow Page 4 - What`s the Address? Pointers
FaxWave - Free Trial.
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Rational Software Development Conference
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PRACTICES

What`s the Address? Pointers
By: McGraw-Hill/Osborne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    2005-09-01

    Table of Contents:
  • What`s the Address? Pointers
  • Assigning a Pointer the Address of a Variable or Constant
  • The Array Name as a Constant Pointer
  • Incrementing a Pointer
  • Passing an Array Using Pointer Notation
  • Dynamic Memory Allocation
  • Returning Pointers from Function s
  • Summary

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    The Best Selling PC Migration Utility.
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    What`s the Address? Pointers - Incrementing a Pointer
    (Page 4 of 8 )

    An important reason for declaring a variable pointer so it points to the same address as the array name is so the variable pointer can be incremented, unlike the array name which cannot be incremented because it is a constant pointer. The fol-lowing program increments the variable pointer to access each succeeding element of the array:


    Figure 11-2.  Variable and constant pointers used to access array elements

    #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++, iPtr++)
      {
        cout << "The address of index " << i << " of the array is "<< iPtr << endl;
        cout << "The value at index " << i << " of the array is "<< *iPtr << endl;
      }
      return 0;
    }

    Incrementing an integer variable increases its value by 1. However, incrementing a pointer variable increases its value by the number of bytes of its data type. This is an example of pointer arithmetic. When you run this program, the first address outputted is 0012FECC, the second 0012FED0, and the third 0012FED4. These hexadecimal addresses are 4 bytes apart because, on the compiler and operating system used by me to run this program, the integer data type takes 4 bytes.

    For this reason, as shown in Figure 11-3, iPtr + 1 is not the base address plus 1, but instead is thebaseaddress+4. Thesameistrueof testScore + 1. Consequently, the value at the second element of the array can be expressed one of four ways:

    • testScore[1];
    • *(testScore + 1);
    • iPtr[1];
    • *(iPtr + 1);

     

    Comparing Addresses

    Addresses can be compared like any other value. The following program modifies the previous one by incrementing the variable pointer so long as the address to which it points is either less than or equal to the address of the last element of the array, which is &testScore[MAX - 1]:

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

    As Figures 11-2 and 11-3 depict, the comparison to &testScore[MAX-1] instead could have been made to testScore + MAX – 1.

    Decrementing a Pointer

    The same considerations apply to decrementing a pointer, which decreases its value by the number of bytes of its data type. Decrementing a pointer can be used to step “backwards” through an array. 

    #include <iostream>> using namespace std; const int MAX = 3;

    int main ()
    {
      int testScore[MAX] = {4, 7, 1};
      int* iPtr = &testScore[MAX - 1];
      int i = MAX - 1; while (iPtr >= &testScore[0])
      {
        cout << "The address of index " << i << " of the array is "<< iPtr << endl;
        cout << "The value at index " << i
    << " of the array is "<< *iPtr << endl;
        iPtr--;
        i--;
      }
      return 0;
    }

    The output is therefore

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

    The key statement is

    int* iPtr = &testScore[MAX - 1];

    This statement has the variable pointer point to the last address in the array. That address then is decremented in the loop so that the pointer variable points to the preceding address in the array. The loop continues so long as the address pointed to by the pointer variable is not before the base address of the array.

    As discussed previously, the pointer variable also could have been initialized as follows:

    int* iPtr = testScore + MAX - 1;

    Pointers as Function Arguments

    Pointers may be passed as function arguments. Pointer notation usually is used to note that an argument is a pointer. However, if the pointer argument is the name of an array, subscript notation alternatively may be used. 

    More Practices Articles
    More By McGraw-Hill/Osborne


       · Got good insight on pointers, i found solution to the bug in my code :)
     

    Buy this book now. This article is taken from chapter 11 of the book C++ Demystified, written by Jeff Kent (McGraw-Hill/Osborne, 2004; ISBN: 0072253703). Check it out at your favorite bookstore today. Buy this book now.

       

    PRACTICES ARTICLES

    - 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
    - Basic Ideas
    - Choosing the Right Team
    - Trees
    - Basic Array Searching in C++
    - Solving Problems with Recursion

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway