Practices
  Home arrow Practices arrow Page 3 - What`s the Address? Pointers
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
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? 
PRACTICES

What`s the Address? Pointers
By: McGraw-Hill/Osborne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 14
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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 - The Array Name as a Constant Pointer
    ( Page 3 of 8 )

    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 Practices Articles          >>> More By McGraw-Hill/Osborne
     

       

    PRACTICES ARTICLES

    - 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
    - Basic Ideas





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT