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
- Start
- Declare an integer variable
- Declare a pointer to the integer variable
- Use the pointer to manipulate the variable
- 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
- Declare and initialize an integer: int a = 10
- Declare a pointer and point it to 'a': int* p = &a
- Manipulate 'a' via 'p'
Tabular Dry Run
val | *p 5 | 5
Output
Value of a: 10 Value pointed by p: 10