C++
  Home arrow C++ arrow Page 3 - What`s the Address? Pointers
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 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? 
C++

What`s the Address? Pointers
By: McGraw-Hill/Osborne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 14
    2005-08-11

    Table of Contents:
  • What`s the Address? Pointers
  • Assigning a Value to a Pointer
  • Pointer as a Variable
  • Incrementing a Pointer
  • Passing an Array Using Pointer Notation
  • Passing a Single Variable Using Pointer Notation
  • Dynamic Memory Allocation
  • Returning Pointers from Functions
  • 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
     
     
    ADVERTISEMENT


    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.

    More C++ Articles
    More By McGraw-Hill/Osborne


     

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

    C++ ARTICLES

    - 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++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek