C++
  Home arrow C++ arrow Page 5 - 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 - Passing an Array Using Pointer Notation


    (Page 5 of 9 )

    In Chapter 10, we employed the following program that used one function to assign values to the array and another function to display values from the array, rather than doing all that work in the main function.

    #include <iostream>
    using namespace std;
    void assignValues(int[], int);
    void displayValues(int[], int);
    const int MAX = 3;
    int main ()
    {
       int testScore[MAX];
       assignValues(testScore, MAX);
       displayValues(testScore, MAX);
       return 0;
    }
    void assignValues(int tests[], int num)
    {
       for (int i = 0; i < num; i++)
       {
         
    cout << "Enter test score #" << i + 1 << ": ";
          cin >> tests[i];
       }
    }
    void displayValues(int scores[], int elems)
    {
     for (int i = 0; i < elems; i++)
       {
          cout << "Test score #" << i + 1 << ": "
             << scores[i] << endl;
       }
    }

    As discussed in Chapter 10, the two functions, assignValues and displayValues, passed their first argument, the array, by address. An argument passed by address can be changed in the calling function (here main) by the called function (here assignValues) just as if the argument had been passed by reference. Thus, the assignValues function changed the value of the testScore array in main by assigning values to the elements of that array.

    The function prototypes and headers of the assignValues and displayValues functions used a subscript [] to indicate that an array is being passed. However, you can also use pointer notation—for instance, asterisk * instead of a subscript []—as the following example demonstrates:

    #include <iostream>
    using namespace std;
    void assignValues(int*, int);
    void displayValues(int*, int);
    const int MAX = 3;
    int main ()
    {
       int testScore[MAX];
       assignValues(testScore, MAX);
       displayValues(testScore, MAX);
       return 0;
    }
    void assignValues(int* tests, int num)
    {
    for (int i = 0; i < num; i++)
      {
        
    cout << "Enter test score #" << i + 1 << ": ";
          cin >> tests[i];
      }
    }
    void displayValues(int* scores, int elems)
    {
    for (int i = 0; i < elems; i++)
      {
         cout << "Test score #" << i + 1 << ": "
            << scores[i] << endl;
      }
    }

    The following comparison of the prototypes of the assignValues function using subscript and pointer notation, respectively, shows that the only difference is whether a subscript [] or an asterisk * is used to denote that the argument is an array:

    void assignValues(int[], int);
    void assignValues(int*, int);

    Similarly, the following comparison of the function headers of the assignValues function using subscript and pointer notation, respectively, shows that the only difference is whether a subscript [] or an asterisk * is used to denote that the argument is an array. This time, however, the asterisk precedes the variable name, whereas the subscript follows the variable name.

    void assignValues(int tests[], int num)
    void assignValues(int* tests, int num)

    Whether you use subscript or pointer notation to pass an array really is a matter of preference. There is no programming advantage one way or the other. However, the next section discusses a situation in which subscript notation is not an option, so pointer notation is the only choice.

    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 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek