Multi-Pointer Implementation: CSU1051P - Shoolini U

Implementation of Multi Pointers

Right Page

Objective

To understand the implementation of multi-pointers in C++

#include <iostream>
int main() {
    // Declare and initialize a variable
    int val = 5;

    // Declare a pointer to the variable
    int* ptr = &val;

    // Declare a double pointer to the pointer
    int** dptr = &ptr;

    // Print the value of the variable, dereferencing the pointer, and double dereferencing the double pointer
    std::cout << "Value of val is " << val << std::endl;
    std::cout << "Value pointed by ptr is " << *ptr << std::endl;
    std::cout << "Value pointed by dptr is " << **dptr << std::endl;

    return 0;
}

Discussion of Algorithm

  1. Start
  2. Declare and initialize a variable
  3. Declare a pointer to the variable
  4. Declare a double pointer to the pointer
  5. Print the value of the variable, dereferencing the pointer, and double dereferencing the double pointer
  6. End

Left Page

Representation using Flow Diagram

   +----------------------------------+
   |                                  |
   |           Start                  |
   |             |                    |
   +----------------------------------+
                  |
                  V
   +----------------------------------+
   |                                  |
   |    Declare and initialize val    |
   |             |                    |
   +----------------------------------+
                  |
                  V
   +----------------------------------+
   |                                  |
   |      Declare pointer ptr         |
   |             |                    |
   +----------------------------------+
                  |
                  V
   +----------------------------------+
   |                                  |
   | Assign address of val to ptr     |
   |             |                    |
   +----------------------------------+
                  |
                  V
   +----------------------------------+
   |                                  |
   |   Declare double pointer dptr    |
   |             |                    |
   +----------------------------------+
                  |
                  V
   +----------------------------------+
   |                                  |
   | Assign address of ptr to dptr    |
   |             |                    |
   +----------------------------------+
                  |
                  V
   +----------------------------------+
   |                                  |
   |   Print value of val              |
   |             |                    |
   +----------------------------------+
                  |
                  V
   +----------------------------------+
   |                                  |
   | Print value pointed by ptr       |
   |             |                    |
   +----------------------------------+
                  |
                  V
   +----------------------------------+
   |                                  |
   | Print value pointed by dptr      |
   |             |                    |
   +----------------------------------+
                  |
                  V
   +----------------------------------+
   |                                  |
   |              Exit                |
   |             |                    |
   +----------------------------------+

Tabular Dry Run

val | *ptr | **dptr | output
 5  |   5  |    5   | print value of val, *ptr, *dptr

Output

Value of val is 5
Value pointed by ptr is 5
Value pointed by dptr is 5