Explain in details what is advantage of pointers over references in c++?

  • Pointers can be reassigned while references cannot. Let us see below code
  • pointer.cpp ( below code )

The above program will compile successfully.

  • refere.cpp

  • From the output it is clear that a reference always point to referent b.
  • We cannot do arithmetic operation on reference.

In the above program even if we increments the reference variable a using a = a + 1 , the value of a would not be incremented. Any operation on a would do effect on the variable b as reference variable a is referring to variable b.

Related Contents to follow