Code About Us Tutorial


  Pointers and References
    What are Pointers?
    Declaring Pointers
    The Indirection and Dereference operator
    What is a Reference?
    Using References
    Using Pointers and References
    The new Keyword
    The delete Keyword
    Passing By Reference
    Passing by Value


   
 

Home > Pointers and References > The Indirection and Dereference operator

November 28, 2009 5:50 pm

   

The Indirection and Dereference operator

The ( * ) operator has two names, and they both fufill different roles. For example, it can be called the indirection operator, if it is used like this:

 

 
int Age = 16;      		 // created a variable
int * pAge = &Age;  	// made a pointer to Age

Indirection means accessing the variables value at the address held by a pointer.

The above program used indirection also. As you can see the following code lines:

 

 
int n = 10;
int*p = &n;

Okay, now I think you understand what indirection is, so we will move on to the next meaning of the ( * ) operator. The indirection operator ( * ) can also be called the dereference operator. Here is an example:

 

 
#include <iostream.h>

int main() {

int Age = 54; int *pAge = &Age; //Indirection operator being used…

cout <<"Here is the value of Age: " << Age << "n"; cout <<"Changing the value of Age using the dereference operator: " << "n"; *pAge = 4; //dereferencing

cout <<"New value of Age: " << Age << endl;

return 0; }

In this little program, I created a variable then used the Indirection operator to create a pointer and gave the pointer the address of Age. This means, whatever I change the pointer too, using the dereference operator ( * ), will change the value of the address its holding. In this case, the address the pointer was holding was the variable Age.

This last note will clear everything up I hope. The indirection operator declares a pointer, but the dereferencing operator gets the value at the address. Please Rate this Code:


    Comments for: The Indirection and Dereference operator
Annonymouse User says:
Indirection and dereferenciation are conceptually the same thing. Traditional C books use both expressions mixed up. C++ and Java books prefer the latter. But I agree that the asterisk in the declaration of a pointer has not the same meaning as the asterisk we use to access the value the pointer points to. In my humble opinion reference parameters don\'t solve the problem neither. That\'s why I continue to explain my students the difference between a call by value using a pointer and using a reference. In this case the difference is clear: you don\'t have to use the indirection operator to access the parameter\'s or variable\'s value. You seem to access it directly.

MoMad [big_mo_mine@yahoo.com] Posted: 31 times. says:
You know, you are right... But, this section is meant to seperate the difference between *pAge++ and int* pAge.

... In this case the difference is clear: you don\'t have to use the indirection operator to access the parameter\'s or variable\'s value. You seem to access it directly.
width=1
width=1

I dont understand what you mean here. Are you talking about: *pAge = 4;

cheesehead [someone@somewhere.com] Posted: 4 times. says:
INCITS/ISO/IEC 14882-2003 states "The unary * operator performs indirection"

I would argue that the * in a declaration/definition is simply a punctuator, but used within an expression it is the indirection operator (of which the term "dereference operator" could be considered a synonym).

However, "dereference operator" is not in the C++ standard, while "indirection operator" can be found in the index, and in "5.3.1 Unary operators".





Add Comment:
Name:
Email:


 «2» THINKQUEST TEAM C0111571 © 2001. All Rights Reserved.