Programming Fundamentals/Indirection Operator

From Wikibooks, open books for an open world
Jump to navigation Jump to search

An introduction to the indirection operator as used within the C++ programming language.

Indirection Operator in C++[edit | edit source]

When we pass parameters to functions we usually pass by value; that, is the calling function provides several values to the called function as needed. The called function takes these values, which have local scope, and stores them on the stack, using them as needed for whatever processing the functions accomplishes. This is the preferred method when calling user defined specific task functions. The called function passes back a single value as the return item if needed. This has the advantage of a closed communications model with everything being neatly passed in as values and any needed item returned back as a parameter.

By necessity there are two exceptions to this closed communications model:

  1. When we need more than one item of information returned by the function
  2. When a copy of an argument cannot reasonably or correctly be made (example: file stream objects).

These exceptions can be handled by parameter passing by reference instead of passing by value.

Parameters can be passed by reference using reference variables or pointer variables, with pointer variables requiring use of the indirection operator. In C++, the asterisk is used as an indirection operator. When a variable is marked with the indirection operator, the value pointed at by the address in the variable, rather than the value of the variable (an address) is indicated. The process of referring to a value at a specific location in memory using the indirection operator is also known as dereferencing.

Example 1: parameter passing with pointers[edit | edit source]

// prototype
void process_values(int qty_dimes, int qty_quarters, double * ptr_value_dimes, double * ptr_value_quarters);

// variable definitions
int            dimes = 45;
int            quarters = 33;
double     value_dimes;
double     value_quarters;
double * ptr_value_dimes = &value_dimes;
double * ptr_value_quarters = &value_quarters;

// somewhere in the function main
process_values(dimes, quarters, ptr_value_dimes, ptr_value_quarters);

// definition of the function
void process_values(int qty_dimes, int qty_quarters, double * ptr_value_dimes, double * ptr_quarters);
{
    * ptr_value_dimes = dimes * 0.10;
    * ptr_value_quarters = quarters * 0.25;
}

NOTE:[edit | edit source]

The asterisk and must appear in both the prototype and the function definition when defining the pointer variables but it does not appear in the function call when the pointers are passed into the function.

The above example shows the basic mechanics of the indirection operator.

The use of pointers with indirection is often preferred for processing arrays. The array index operator is also known as the array method of dereferencing. The following couts are equivalent:

int ages[] = {47, 45, 18, 11, 9};

cout << ages[3];

cout << *(ages + 3);

They both say, "The name of an array is a pointer; take the pointer and calculate a new address that points to the 3rd offset by adding the correct number of bytes onto the pointer (integer data type is normally 4 bytes long – 3 offsets times 4 bytes is 12 bytes); then dereference that pointer (since this is an Rvalue context – fetch me the value that you are pointing at) and send it to the standard output device."

You should study the demonstration programs in conjunction with this module.

Demonstration Program in C++[edit | edit source]

Creating a Folder or Sub-Folder for Source Code Files[edit | edit source]

Depending on your compiler/IDE, you should decide where to download and store source code files for processing. Prudence dictates that you create these folders as needed prior to downloading source code files. A suggested sub-folder for the Bloodshed Dev-C++ 5 compiler/IDE might be named:

  • Demo_Programs

If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.

Download the Demo Program[edit | edit source]

Download and store the following file(s) to your storage device in the appropriate folder(s). Following the methods of your compiler/IDE, compile and run the program(s). Study the source code file(s) in conjunction with other learning materials. You may need to right click on the link and select "Save Target As" in order to download the file.

Download from Connexions: Demo_Pointer_Passing.cpp

Download from Connexions: Demo_Array_ Pointer_Processing.cpp

Definitions[edit | edit source]

indirection operator
The asterisk used for dereferencing a pointer.
dereferencing
The concept of using the item to which a pointer or address is pointing at.