Home arrow C++ arrow Page 6 - What`s the Address? Pointers
C++

What`s the Address? Pointers


A pointer simply points to another variable or constant. Though they have a reputation for being difficult to learn, they really are not that hard. This is fortunate, becuase pointers are very important to programming. This article explains how to create and work with pointers in C++. It is excerpted from chapter 11 of the book C++ Demystified, written by Jeff Kent (McGraw-Hill, 2004; ISBN: 0072253703).

Author Info:
By: McGraw-Hill/Osborne
Rating: 5 stars5 stars5 stars5 stars5 stars / 27
August 11, 2005
TABLE OF CONTENTS:
  1. · What`s the Address? Pointers
  2. · Assigning a Value to a Pointer
  3. · Pointer as a Variable
  4. · Incrementing a Pointer
  5. · Passing an Array Using Pointer Notation
  6. · Passing a Single Variable Using Pointer Notation
  7. · Dynamic Memory Allocation
  8. · Returning Pointers from Functions
  9. · Summary

print this article
SEARCH DEVARTICLES

What`s the Address? Pointers - Passing a Single Variable Using Pointer Notation
(Page 6 of 9 )

Passing an array name by address is relatively simple because the value of the array name is an address. However, you may often want to pass a single variable by address. By single variable I donít mean a variable that is unmarried, but instead, for example, an int as opposed to an int array.

With a single variable, subscript notation is not an option. Subscripts make sense only with an array. Rather, you need to use pointer notation to pass a single variable by address.

Passing an argument by reference or by address both enable the passed argument to be changed in the calling function by the called functionóonly the syntax is different. For comparison, letís start with the following program from Chapter 9 that passes the variable to be doubled by reference:

#include <iostream>
using namespace std;
void doubleIt(int&);
int main ()
{
   int num;
   cout << "Enter number: ";
   cin >> num;
   doubleIt(num);
   cout << "The number doubled in main is " << num << endl;
   return 0;
}
void doubleIt (int& x)
{
  
cout << "The number to be doubled is " << x << endl;
   x *= 2;
   cout << "The number doubled in doubleIt is " << x << endl;
}

Here is some sample input and output:

Enter number: 3
The number to be doubled is 3
The number doubled in doubleIt is 6
The number doubled in main is 6

Letís now modify this program so it passes the variable to be doubled by address instead of by reference:

#include <iostream>
using namespace std;
void doubleIt(int*);
int main ()
{
   int num;
   cout << "Enter number: ";
   cin >> num;
   doubleIt(&num);
   cout << "The number doubled in main is " << num << endl;
   return 0;
}
void doubleIt (int* x)
{
  
cout << "The number to be doubled is " << *x << endl;
   *x *= 2;
   cout << "The number doubled in doubleIt is " << *x << endl;
}

There are four syntax differences between these two programs.

  1. In the function prototype, you use an ampersand (&) for passing by reference but an asterisk (*) for passing by address:

    void doubleIt(int&);  // by reference
    void doubleIt(int*);  // by address
  2. Similarly, in the function header, you use an ampersand (&) for passing by reference but an asterisk (*) for passing by address:

    void doubleIt (int& x)   // by reference
    void doubleIt (int* x)   // by address
  3. When you call the function, you donít need the address operator (&) for passing by reference, but you do need one for passing by address since you are supposed to be passing by the address of x.:

    doubleIt(num);   // by reference
    doubleIt(&num);  // by address
  4. In the body of the called function, you donít need to dereference the argument when you pass it by reference, but you do need to when you pass by address since x, being passed by address, is not a value but is instead a pointer:

// by reference -no dereference
{
   cout << "The number to be doubled is " << x << endl;
   x *= 2;
   cout << "The number doubled in doubleIt is " << x << endl;
}
// by address -need to dereference
{
  
cout << "The number to be doubled is " << *x << endl;
   *x *= 2;
   cout << "The number doubled in doubleIt is " << *x << endl;
}

You may legitimately be wondering why, with a single variable argument, I would want to pass it by address when the syntax for passing it by reference seems easier. The pat answer I give my students is that there are certain sadistic computer science teachers (Iím not mentioning any names here) who insist their students pass by address to make them suffer. All kidding aside though, there are actually certain library functions that do use pass by address. Additionally, when using dynamic memory allocation and returning pointers from functions (to be covered in the following sections), passing by address may be the only option.


blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- 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++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials