Skip to main content

More about Pointer in C++

 

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int a = 2;
    int *p = &a;
    cout << a << endl;  // It will print the value of a
    cout << &a << endl; // It will print the Address of a
    cout << p << endl;  // It will print the Address of a
    cout << &p << endl; // It will print the Address of p
    cout << *p << endl; // It will print the value of a

    int *ptr = new int(4);
    cout << *ptr << endl; // It will print the value of

    int *b = new int[3];
    b[0] = 1;
    b[1] = 2;
    b[2] = 3;
    delete[] b;
   // b[2] = 4;
    cout<<"b[0] = "<<b[0]<<endl;
    cout<<"b[1] = "<<b[1]<<endl;
    cout<<"b[2] = "<<b[2]<<endl;
   
    // delete operator

   
    return 0;
}

Comments