Skip to main content
Engineering LibreTexts

12.3: Indirection Operator

  • Page ID
    29105
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Indirection Operator in C++

    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:

    When we need more than one item of information returned by the function

    When a copy of an argument cannot reasonably or correctly be made (example: file stream objects).

    These exceptions could be handled by parameter passing by reference instead of passing a value. Although different syntax than parameter passing when using a reference variable; using a pointer variable and the indirection operator can accomplish the same effect. The indirection operator is the asterisk or the character that we also use for multiplication. The concept of indirection is also known as dereferencing, meaning that we are not interested in the pointer but want the item to which the address is referring or referencing.

    Parameter passing with pointers
    // 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: 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);

    The 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.

    Definitions

    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.

    This page titled 12.3: Indirection Operator is shared under a CC BY-SA license and was authored, remixed, and/or curated by Patrick McClanahan.