DEV Community

Script Koder
Script Koder

Posted on

DSA Using C++

Starting with Pointers


Code without Pointers

# include <iostream>

using namespace std;

int main() {
    // defining number 1
    int num1 = 11;
    // defining number 2
    int num2 = num1;

    // redefining num1
    num1 = 22;

    // print out the num1 and num2
    cout << "num1 = " << num1 << endl;
    cout << "num2 = " << num2 << endl;

}
Enter fullscreen mode Exit fullscreen mode
  • Result : value will be different in both num1 and num2

Code with Pointers

# include <iostream>

using namespace std;

int main() {

    // defining num1 using pointer
    int* num1 = new int(11);

    // defining num2 using pointer
    int* num2 = num1;

    // print out the num1 and num2
    cout << "num1 = " << *num1 << endl;
    cout << "num2 = " << *num2 << endl;

}
Enter fullscreen mode Exit fullscreen mode
  • Result : value will be same in both num1 and num2

Top comments (0)