Breaking News

How to use the Pointer variable in easy way in C++

Pointer is a variable that can store a value and address of another variable. This concept is very easy to implement in C++.
We use an ampersand operator (&) to get the memory address of a certain variable. Lets look by Example, how this can happen....!!!
I'm using Dev-C++ Compiler to compile and run my code but don't worry, any compiler these codes can work.


  • Open your Compiler
  • Now, start to type the following piece of code include libraries if needed
          #include <iostream>
          using namespace std;
          int main()
          {
          int num=5;
          return 0;
          }



  • In that piece of code, we have initialize the variable num with the value 5 inside on it (Initialization).
  • Now we can get the Memory Address of the variable num by using an ampersand operator ( ). Below of int num=5; type the following code to print (display) it.
  • cout << &num; (This ( & ) operator will fetch the memory address of the variable num  and display it on screen)
          #include <iostream>
          using namespace std;
          int main()
          {
          int num=5;
                  cout << &num;
          return 0;
          }
  • When I run this program, it will display the following output.

  • As we can see in the picture above, the memory Address of the variable num is 0x6ffe4c. (This address is represented by hexadecimals format).
  • Now, lets take this variable and store to another variable which is a pointer variable. Follow the following steps to perform that task.
  • Declare another pointer variable first, then we will use it to store the memory Address. To declare it, add the following line of code.
  • int *numPtr; (When we use asterisk sign ( * ) first of variable, it means we declare a pointer variable)

          #include <iostream>
          using namespace std;
          int main()
          {
          int *numPtr
                  int num=5;
                  cout << &num;
          return 0;
          }


  • Now, we can store the Memory Address of the variable num to a pointer variable *numPtr, just add the following line of code below of int num=5;
  • numPtr= &num;

          #include <iostream>
          using namespace std;
          int main()
          {
          int *numPtr
                  int num=5;
                  numPtr= &num;
          return 0;
          }

  • Until here, we have store the Memory Address of the variable num to a pointer variable numPtr. Just remember, when we use an ampersand operator ( & ), we fetch the memory address of a certain variable, so on the above codes show that, the fetched Memory Address has been stored to numPtr variable.
  • Lets see if it work by adding the last line of code that will display it on screen. Below numPtr=&num; add the following line of code and run your program.
  • cout << numPtr;

          #include <iostream>
          using namespace std;
          int main()
          {
          int *numPtr
                  int num=5;
                  numPtr= &num;
                  cout << numPtr;
          return 0;
          }
  • When I run this program, it will display the following output.
  • As you can see, the Memory Address of the num variable has been stored to a pointer variable numPtr. Remember, from the definition of a pointer variable we said that it can store value and memory address of other variable, so how we can get the value of that memory Address? Its simple, just put asterisk sign ( * ) on pointer variable in cout statement.
  • cout << *numPtr; (This line will display the value of the Memory Address stored in numPtr variable).

          #include <iostream>
          using namespace std;
          int main()
          {
          int *numPtr
                  int num=5;
                  numPtr= &num;
                  cout << numPtr <<endl;
                  cout << *numPtr;
          return 0;
          }
  • When I run this program, it will display the following output.
  • We are done, I hope now you understand how a pointer variable work. Feel free to ask question if you didn't understand then I can reply as faster as I can.

Please, comment and share with your friends.
Thank you...!!!

No comments