Practices
  Home arrow Practices arrow Page 2 - 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
     
     
    CIO Insight
     
    ADVERTISEMENT

    PCmover - $15 Off with Coupon Code CJPH7Q

    What`s the Address? Pointers - Assigning a Pointer the Address of a Variable or Constant
    (Page 2 of 8 )

    Let’s now assign a pointer a “real” value, the address of another variable or constant. To do so, you need to access the address of the variable or constant before you can assign that address to the pointer. You use the address operator, covered in Chapter 3, to accomplish this task.

    The following program shows how to use the address operator to assign the address of a variable to a pointer. This program also demonstrates that the value of a pointer is the same as the address to which the pointer points.

    #include <iostream>> using namespace std;

    int main ()

      int num = 5;
      int* iPtr = &num;
      cout << "The address of x using &num is " << &num << endl;
      cout << "The address of x using iPtr is " << iPtr << endl;
      return 0;
    }

    The output on my computer (the following addresses likely will be different on yours) is

    The address of x using &num is 0012FED4
    The address of x using iPtr is 0012FED4


    Figure 11-1 shows graphically how the pointer points to the integer variable.

    Indirection Operator and Dereferencing

    The primary use of a pointer is to access and, if appropriate, change the value of the variable that the pointer is pointing to. In the following program, the value of the integer variable num is changed twice.

    #include <iostream>> using namespace std;

    int main ()
    {
      int num = 5;
      int* iPtr = &num;
      cout << "The value of num is " << num << endl;
      num = 10;
      cout << "The value of num after num = 10 is "
    << num << endl;
      *iPtr = 15; 
      cout << "The value of num after *iPtr = 15 is "
    << num << endl;
      return 0;
    }

    The resulting output is

    The value of num is 5
    The value of num after num = 10 is 10
    The value of num after *iPtr = 15 is 15

    The first change should be familiar, by the direct assignment of a value to num, such as num=10. However, the second change is accomplished a new way, using the indirection operator:

    *iPtr = 15;

    The indirection operator is an asterisk, the same asterisk that you used to declare the pointer or to perform multiplication. However, in this statement the asterisk is not being used in a declaration or to perform multiplication, so in this context it is being used as an indirection operator.

    NOTE: As mentioned earlier in this chapter, this is another example of a symbol having different meanings in the C++ programming language depending on the context in which it was used.

    The placement of the indirection operator before a pointer is said to dereference the pointer. Indeed, some texts refer to the indirection operator as the dereferencing operator. The value of a dereferenced pointer is not an address, but rather the value at that address—that is, the value of the variable that the pointer points to.

    For example, in the preceding program, iPtr’s value is the address of num. However, the value of iPtr dereferenced is the value of num. Thus, the following two statements have the same effect, both changing the value of num:

    num = 25;
    *iPtr = 25;

    Similarly, a dereferenced pointer can be used in arithmetic expressions the same as the variable to which it points. Thus, the following two statements have the same effect:

    num *= 2;
    *iPtr *= 2;

    In these examples, changing a variable’s value using the indirection operator rather than through a straightforward assignment seems like an unnecessary complication. However, there are instances covered later in this chapter, such as looping through an array using a pointer, or using dynamic memory allocation, in which using the indirection operator is helpful or even necessary.

    The Pointer as a Variable or a Constant

    A pointer may be a variable or a constant. Let’s examine both possibilities.

    Pointer as a Variable

    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

    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




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