Pointer Implementation: CSU1051P - Shoolini U

Implementation of Pointers

To understand the implementation of pointers using C++

Objective

To understand the implementation of pointers using C++

#include <iostream>
int main() {
    // Declare an integer variable
    int a = 10;

    // Declare a pointer to the integer variable
    int* p = &a;

    // Use the pointer to print the value
    std::cout << "Value of a: " << a << std::endl;
    std::cout << "Value pointed by p: " << *p << std::endl;

    return 0;
}

Discussion of Algorithm

  1. Start
  2. Declare an integer variable
  3. Declare a pointer to the integer variable
  4. Use the pointer to manipulate the variable
  5. End

Representation


Flow Diagram

   +----------------------------------+
   |                                  |
   |           Start                  |
   |             |                    |
   +----------------------------------+
                  |
                  V
   +----------------------------------+
   |                                  |
   |   Declare and initialize a       |
   |             |                    |
   +----------------------------------+
                  |
                  V
   +----------------------------------+
   |                                  |
   |    Declare pointer p             |
   |             |                    |
   +----------------------------------+
                  |
                  V
   +----------------------------------+
   |                                  |
   | Assign address of a to p         |
   |             |                    |
   +----------------------------------+
                  |
                  V
   +----------------------------------+
   |                                  |
   |   Print value of a                |
   |             |                    |
   +----------------------------------+
                  |
                  V
   +----------------------------------+
   |                                  |
   | Print value pointed by p         |
   |             |                    |
   +----------------------------------+
                  |
                  V
   +----------------------------------+
   |                                  |
   |             Exit                 |
   |             |                    |
   +----------------------------------+

Dry Run

  1. Declare and initialize an integer: int a = 10
  2. Declare a pointer and point it to 'a': int* p = &a
  3. Manipulate 'a' via 'p'

Tabular Dry Run

val | *p
 5  |  5

Output

Value of a: 10
Value pointed by p: 10