C++
  Home arrow C++ arrow Page 2 - 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  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
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 - Assigning a Value to a Pointer


    (Page 2 of 9 )

    This section will explain how you assign a value to a pointer. Though, before I explain how, perhaps I should explain why.

    Why You Should Not Try to Use an Unassigned Pointer

    Back in elementary school we were taught a verse: “I shot an arrow into the air, where it lands, I don’t care.” Looking back, I wonder why young children were taught this verse. It may rhyme, but its message is really not appropriate for little ones. However, when you declare a pointer but then use it without first assigning it a value, you are, alas, doing the programming equivalent of that verse.

    The following program declares a pointer and then attempts to output its value without first assigning it a value:

    #include <iostream>
    using namespace std;
    int main ()
    {
     
    int* iPtr;
     
    cout << "The value of iPtr is " << iPtr << endl;
      return 0;
    }

    The result, depending on your compiler and operating system, may be a compiler error, a runtime error, or a computer that locks up. Regardless, attempting to use a declared pointer without first assigning it a value is not a good idea.

    As you may recall from previous chapters, when you declare a variable and then attempt to output its value without first assigning it a value, the result is a so-called “garbage value” that makes little sense. The reason for this result is that the computer attempts to interpret whatever value is left over from previous programs at the address of the variable.

    When the variable is a pointer, that leftover value is interpreted as another memory address, which the pointer then tries to access when you attempt to use it. There are a number of memory address ranges that you are not permitted to access programmatically, such as those reserved for use by the operating system. If the leftover value is interpreted as one of those prohibited addresses, the result is an error.

    Null Pointers

    If it is too early in your code to know which address to assign to the pointer, then you first assign the pointer NULL, which is a constant with a value of zero defined in several standard libraries, including iostream. The following program does so:

    #include <iostream>
    using namespace std;
    int main ()
    {
      
    int* iPtr;
      
    iPtr = NULL;
      
    cout << "The value of iPtr is " << iPtr << endl;
      
    return 0;
    }


    NOTE:   You also could use initialization instead of declaration followed by assignment, thus combining the first two statements in main to int* iPtr = NULL.

    The resulting output is

    The address of x using iPtr is 00000000

    A pointer that is assigned NULL is called a null pointer.

    On most operating systems, programs are not permitted to access memory at address 0 because that memory is reserved by the operating system. You may now be thinking: “Wait a minute! He just told me how bad it was to risk having pointers point to memory addresses reserved by the operating system. Now he’s having us do that on purpose.” However, the memory address 0 has special significance; it signals that the pointer is not intended to point to an accessible memory location. Thus, if it is too early in your code to know which address to assign to a pointer, you should first assign the pointer to NULL, which then makes it safe to access the value of a pointer before it is assigned a “real” value such as the address of another variable or constant.

    Assigning a Pointer the Address of a Variable or Constant

    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.


    Figure 11-1.  Pointer pointing to an 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.

    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

    - Multiplying Large Numbers with Karatsuba`s A...
    - Large Numbers
    - Dijkstra`s Shunting Algorithm with STL and C...
    - Brief Introduction to the STL Containers
    - The Standard Template Library
    - Templates in C++
    - C++ Programmer Alerts
    - C++ Programming Tips
    - First Steps in (C) Programming, conclusion
    - First Steps in (C) Programming, continued
    - First Steps in (C) Programming, introduction
    - C++ Preprocessor: Always Assert Your Code Is...
    - C++ Preprocessor: The Code in the Middle
    - Programming in C
    - Temporary Variables: Runtime rvalue Detection






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